#line 2 "kyopro_lib/template/template.hpp" #include using namespace std; using ll = long long; #define all(x) (x).begin(), (x).end() template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } ll sum(vector a){ ll res = 0; for(auto x:a) res += x; return res; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { ll s = (ll)v.size(); for (ll i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } #define rep(i, n) for (ll (i) = 0; (i) < (ll)(n); (i)++) #line 2 "main.cpp" signed main(){ ll H,W,N; cin >> H >> W >> N; H--;W--; map,ll> mp; priority_queue,pair>>, vector,pair>>>, greater,pair>>>> q; rep(i,N) { ll a,b,c,d; cin >> a >> b >> c >> d; a--;b--;c--;d--; q.push({a+b,{{a,b},{c,d}}}); } ll finaly = H+W; while(!q.empty()) { auto [_,p] = q.top(); q.pop(); auto [a,b] = p.first; auto [c,d] = p.second; if (mp[{a,b}] == 0) { mp[{a,b}] = a+b; }else { mp[{a,b}] = min(a+b,mp[{a,b}]); } if (mp[{c,d}] == 0) { mp[{c,d}] = min(c+d,mp[{a,b}]+1); }else { mp[{c,d}] = min(c+d,min(mp[{c,d}],mp[{a,b}]+1)); } finaly = min(finaly,H+W-c-d+mp[{c,d}]); finaly = min(finaly,H+W-a-b+mp[{a,b}]); } cout << finaly << endl; }