#include using namespace std; #define int long long using Graph=vector>>; vector dijkstra(vector>> G,int v){ int N=(int)(G.size()); vector cur(N,2000000000000LL); vector kakutei(N,false); priority_queue,vector>,greater>> que; cur[v]=0; que.push(make_pair(cur[v],v)); while(!que.empty()){ int pos=que.top().second; que.pop(); if(kakutei[pos]==true){ continue; } kakutei[pos]=true; for(pair i : G[pos]){ int nex=i.first; int cost=i.second; if(cur[nex]>cur[pos]+cost){ cur[nex]=cur[pos]+cost; que.push(make_pair(cur[nex],nex)); } } } return cur; } signed main(){ int H,W,N; cin >> H >> W >> N; //辺数O(N^2)本,頂点数O(N)の重み付き最短経路 //dijkstraでO(N^2 log N) Graph G(2*N+2); vector> start(N); vector> goal(N); for(int i=0;i> start[i].first >> start[i].second >> goal[i].first >> goal[i].second; } G[0].push_back(make_pair(2*N+1,H-1+W-1)); G[2*N+1].push_back(make_pair(0,H-1+W-1)); for(int i=0;i cur=dijkstra(G,0); cout << cur[2*N+1] << endl; }