#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long H,W; cin >> H >> W; int N; cin >> N; map,pair> Graph; map,int> dist; for(int i=0; i> a >> b >> c >> d; Graph[{a,b}] = {c,d}; dist[{a,b}] = 2e9+9; dist[{c,d}] = 2e9+9; } dist[{1,1}] = 0; dist[{H,W}] = 2e9+9; priority_queue,vector>,greater<>> Q; Q.push({0,1,1}); while(Q.size()){ auto [d,x,y] = Q.top(); Q.pop(); if(dist[{x,y}] > d) continue; auto [x2,y2] = Graph[{x,y}]; if(x2 != 0) if(dist[{x2,y2}] > d+1) dist[{x2,y2}] = d+1,Q.push({d+1,x2,y2}); for(auto [p,d3] : dist){ auto [x3,y3] = p; long long nc = d; nc += abs(x-x3); nc += abs(y-y3); if(nc < d3) dist[p] = nc,Q.push({nc,x3,y3}); } } cout << dist[{H,W}] << endl; }