#include using namespace std; const long long INF = 1000000000000; int main(){ int H, W, N; cin >> H >> W >> N; vector a(N), b(N), c(N), d(N); for (int i = 0; i < N; i++){ cin >> a[i] >> b[i] >> c[i] >> d[i]; a[i]--; b[i]--; c[i]--; d[i]--; } vector x(N * 2 + 2), y(N * 2 + 2); for (int i = 0; i < N; i++){ x[i * 2] = a[i]; y[i * 2] = b[i]; x[i * 2 + 1] = c[i]; y[i * 2 + 1] = d[i]; } x[N * 2] = 0; y[N * 2] = 0; x[N * 2 + 1] = H - 1; y[N * 2 + 1] = W - 1; vector used(N * 2 + 2, false); vector D(N * 2 + 2, INF); D[N * 2] = 0; for (int i = 0; i < N * 2 + 2; i++){ int p = -1; for (int j = 0; j < N * 2 + 2; j++){ if (!used[j]){ if (p == -1){ p = j; } else if (D[j] < D[p]){ p = j; } } } used[p] = true; for (int k = 0; k < N * 2 + 2; k++){ D[k] = min(D[k], D[p] + abs(x[k] - x[p]) + abs(y[k] - y[p])); } if (p < N * 2 && p % 2 == 0){ D[p + 1] = min(D[p + 1], D[p] + 1); } } cout << D[N * 2 + 1] << endl; }