#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include using namespace std; const ll INF=1LL<<60; struct Edge{ int to; ll w; Edge(int to,ll w) : to(to),w(w) {} }; using Graph=vector>; using pli=pair; template bool chmin(T& a,T b){ if(a>b){ a=b; return true; } return false; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll h,w,n; cin>>h>>w>>n; vector A(n),B(n),C(n),D(n); rep(i,n){ cin>>A[i]>>B[i]>>C[i]>>D[i]; A[i]--,B[i]--,C[i]--,D[i]--; } Graph G(2*n+2); rep(i,n){ G[i].push_back(Edge(i+n,1)); } G[2*n].push_back(Edge(2*n+1,h+w-2)); rep(i,n){ G[2*n].push_back(Edge(i,A[i]+B[i])); G[i+n].push_back(Edge(2*n+1,h+w-2-C[i]-D[i])); } rep(i,n){ rep(j,n){ G[i].push_back(Edge(j,abs(A[i]-A[j])+abs(B[i]-B[j]))); G[i+n].push_back(Edge(j,abs(C[i]-A[j])+abs(D[i]-B[j]))); } } vector dist(2*n+2,INF); int s=2*n; dist[s]=0; priority_queue,greater> que; que.push({dist[s],s}); while(!que.empty()){ int v=que.top().second; ll d=que.top().first; que.pop(); if(d>dist[v]) continue; for(auto e:G[v]){ if(chmin(dist[e.to],dist[v]+e.w)){ que.push({dist[e.to],e.to}); } } } cout<