#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 110, M = 10010;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
bool st[N][N][M];
int n,m,t;
int main(){
scanf("%d%d%d",&n,&m,&t);
for(int i = 0;i < t;i++){
int a,b,l,r;
scanf("%d%d%d%d",&a,&b,&l,&r);
for(int j = l;j <= r;j++)
st[a][b][j] = 1;
}
queue<pair<int,PII>> q;
q.push({0,{1,1}});
st[1][1][0] = 1;
while(q.size()){
auto t = q.front();
int idx = t.first;
int x = t.second.first,y = t.second.second;
q.pop();
if(x == n && y == m){
cout << idx << endl;
return 0;
}
for(int i = 0;i < 4;i++){
int tx = x + dx[i],ty = y + dy[i];
if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !st[tx][ty][idx + 1]){
q.push({idx + 1,{tx,ty}});
st[tx][ty][idx + 1] = 1;
}
}
}
return 0;
}