#ifndef akinyan #define akinyan #include <bits/stdc++.h> using namespace std; using ch = char; using ll = long long; using ld = long double; using db = double; using st = string; using vdb = vector<db>; using vvdb = vector<vdb>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vd = vector<ld>; using vvd = vector<vd>; using vs = vector<st>; using vvs = vector<vs>; using vc = vector<ch>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; const ll mod = 998244353; const ll MOD = 1000000007; const ll INF = 1000000000000000000LL; using pall = pair<ll,ll>; using vp = vector<pall>; #endif struct Edge { long long to; long long cost; }; using Graph = vector<vector<Edge>>; using P = pair<long, int>; void dijkstra(const Graph &G, int s, vector<long long> &dis) { int N = G.size(); dis.resize(N, INF); priority_queue<P, vector<P>, greater<P>> pq; // 「仮の最短距離, 頂点」が小さい順に並ぶ dis[s] = 0; pq.emplace(dis[s], s); while (!pq.empty()) { P p = pq.top(); pq.pop(); int v = p.second; if (dis[v] < p.first) { // 最短距離で無ければ無視 continue; } for (auto &e : G[v]) { if (dis[e.to] > dis[v] + e.cost) { // 最短距離候補なら priority_queue に追加 dis[e.to] = dis[v] + e.cost; pq.emplace(dis[e.to], e.to); } } } } int main(){ ll H,W,N; cin>>H>>W>>N; Graph G(N+2); vvl X(N,vl(4)); for(int i=0; i<N; i++){ for(int j=0; j<4; j++){ cin>>X[i][j]; } } for(int i=0; i<N; i++){ G[0].push_back({i+1,abs(X[i][0] - 1) + abs(X[i][1] - 1)}); } G[0].push_back({N+1,H+W-2}); for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ if(i != j){ G[i+1].push_back({j+1,abs(X[i][2] - X[j][0]) + abs(X[i][3] - X[j][1]) + 1}); } } G[i+1].push_back({N+1,abs(H - X[i][2]) + abs(W - X[i][3]) + 1}); } vl D; dijkstra(G,0,D); cout << D[N+1] << endl; }