#include #include #include #include using namespace std; using ll = long long; using Graph = vector>>; using pli = pair; struct point{ ll x, y; ll distance(point q){ return abs(x-q.x)+abs(y-q.y); } }; const ll INF = 1LL<<60; int N; point pos[2010]; ll G[2010][2010]; ll D[2010]; void Dijkstra(int src){ fill(&D[0], &D[0]+2010, INF); D[src]=0LL; priority_queue, greater> Q; Q.push({0LL, src}); while(! Q.empty()){ auto [dist, cur] = Q.top(); Q.pop(); if (dist>D[cur]) continue; for (int dst=0;dst<2*N+2;dst++){ if (dist+G[cur][dst]>H>>W>>N; pos[0]={1LL, 1LL}, pos[1]={H, W}; for (int i=1;i<=N;i++){ cin>>pos[2*i].x>>pos[2*i].y>>pos[2*i+1].x>>pos[2*i+1].y; } for (int i=0;i<2*N+2;i++){ for (int j=i;j<2*N+2;j++){ G[i][j]=pos[i].distance(pos[j]); G[j][i]=G[i][j]; } } for (int i=1;i<=N;i++) G[2*i][2*i+1]=1; Dijkstra(0); cout << D[1] << "\n"; return 0; }