#include<bits/stdc++.h> using namespace std; #include<atcoder/all> using namespace atcoder; #define rep(i,n) for (int i = 0; i < (n); ++i) using ld = long double; using ll = long long; template<class T> bool chmax(T &a, T b) { if(a<b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, T b) { if(a>b) { a = b; return true; } return false; } int H, W, N, A[1<<10][4]; using P = pair<ll,ll>; int main() { cin >> H >> W >> N; rep(i,N) rep(j,4) cin >> A[i][j]; int M = 2*N+2; ll G[M][M] = {}; rep(i,M) rep(j,M) G[i][j] = 1e18; rep(i,2*N) rep(j,2*N) if(i!=j) { int a = (i&1?2:0); int b = (j&1?2:0); G[i][j] = abs(A[i/2][a]-A[j/2][b]) + abs(A[i/2][a+1]-A[j/2][b+1]); } rep(i,N) G[2*i][2*i+1] = 1; rep(i,2*N) { int a = (i&1?2:0); G[2*N][i] = G[i][2*N] = A[i/2][a] + A[i/2][a+1] - 2; G[2*N+1][i] = G[i][2*N+1] = H-A[i/2][a] + W-A[i/2][a+1]; } priority_queue<P, vector<P>, greater<P>> q; q.push({0,2*N}); vector<ll> dist(2*N+2, 1e18); dist[2*N] = 0; while(!q.empty()) { auto [c,v] = q.top(); q.pop(); if(dist[v]<c) continue; rep(nv,2*N+2) if(nv!=v && G[v][nv]<1e18) { if(chmin(dist[nv], dist[v]+G[v][nv])) q.push({dist[nv], nv}); } } //rep(i,M) cout << dist[i] << endl; cout << dist[2*N+1] << endl; }