#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
#define N 50000
int dp[101][101][2];
int a[101][101];
int vis[101][101];
int d[4][2] = { { 1, 0 }, { 0, 1 }, { 0, -1 }, { -1, 0 } };
int n, m, t;
bool check(int i, int j)
{
if (i <= n &&j <= m && i > 0 && j > 0)
return true;
return false;
}
bool f(int i, int j,int step)
{
if (a[i][j] == 0)return true;
else if (a[i][j] == 1 && (step < dp[i][j][0] || dp[i][j][1] < step))
return true;
else return false;
}
struct node
{
int x;
int y;
int step;
};
queue<node> qq;
void bfs()
{
//if (qq.empty())
while (!qq.empty())qq.pop();
node q, p;
q.x = 1;
q.y = 1;
q.step = 0;
qq.push(q);
int x, y;
while (!qq.empty())
{
q = qq.front();
vis[q.x][q.y] = q.step;
qq.pop();
if (q.x == n && q.y == m)
{
cout << q.step << endl;
return;
}
for (int i = 0; i < 4; i++)
{
x=q.x + d[i][0];
y = q.y + d[i][1];
if (check(x, y) && q.step + 1 > vis[x][y]&& f(x,y,q.step+1))
{
p.x = x;
p.y = y;
p.step = q.step +1;
qq.push(p);
}
}
}
return;
}
int main()
{
int x, y, c, d;
while (cin >> n >> m >> t)
{
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
for (int i = 0; i < t; i++)
{
cin >> x >> y >> c >> d;
a[x][y] = 1;
dp[x][y][0] = c;
dp[x][y][1] = d;
}
bfs();
}
return 0;
}