#include using namespace std; #define rep(i, a, n) for(int i = a; i < n; i++) #define rrep(i, a, n) for(int i = a; i >= n; i--) #define ll long long #define pii pair #define pll pair // constexpr ll MOD = 1000000007; constexpr ll MOD = 998244353; constexpr int IINF = 1001001001; constexpr ll INF = 1LL<<60; template void chmax(t&a,u b){if(a void chmin(t&a,u b){if(b dijk(ll s, vector> &g){ priority_queue, greater> que; vector dist(g.size(), (1LL<<60)); que.push(make_pair(0, s)); dist[s] = 0; while(!que.empty()){ pll q = que.top(); que.pop(); ll d = q.first, u = q.second; if(dist[u] < d) continue; for(auto nq: g[u]){ ll v = nq.first, cost = nq.second; if(dist[v] > d+cost){ dist[v] = d+cost; que.push({dist[v], v}); } } } return dist; } int main(){ int h, w, n; cin >> h >> w >> n; vector>> g(2*n+2); vector a(n), b(n), c(n), d(n); rep(i, 0, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; rep(i, 0, n){ g[2*i].push_back({2*i+1, 1}); g[2*i+1].push_back({2*i, abs(a[i]-c[i])+abs(b[i]-d[i])}); } rep(i, 0, n){ rep(j, i+1, n){ g[2*i].push_back({2*j, abs(a[i]-a[j])+abs(b[i]-b[j])}); g[2*j].push_back({2*i, abs(a[i]-a[j])+abs(b[i]-b[j])}); g[2*i].push_back({2*j+1, abs(a[i]-c[j])+abs(b[i]-d[j])}); g[2*j+1].push_back({2*i, abs(a[i]-c[j])+abs(b[i]-d[j])}); g[2*i+1].push_back({2*j, abs(c[i]-a[j])+abs(d[i]-b[j])}); g[2*j].push_back({2*i+1, abs(c[i]-a[j])+abs(d[i]-b[j])}); g[2*i+1].push_back({2*j+1, abs(c[i]-c[j])+abs(d[i]-d[j])}); g[2*j+1].push_back({2*i+1, abs(c[i]-c[j])+abs(d[i]-d[j])}); } } rep(i, 0, n){ g[2*n].push_back({2*i, a[i]-1+b[i]-1}); g[2*i].push_back({2*n+1, h-a[i]+w-b[i]}); g[2*i+1].push_back({2*n+1, h-c[i]+w-d[i]}); } g[2*n].push_back({2*n+1, h+w}); vector dist = dijk(2*n, g); cout << dist[2*n+1] << endl; return 0; }