#include #include using mint = atcoder::modint998244353; //using mint = atcoder::modint1000000007; using namespace atcoder; using namespace std; using ll = long long; using pi = pair; using pl = pair; using vi = vector; using vm = vector; using vvi = vector; using vl = vector; using vvl = vector; using vpi = vector>; using vpl = vector>; #define rep(i,n) for (ll i = 0; i < n; i++) #define replr(i,l,r) for (ll i = l; i < r; i++) set abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; const int inf =1e9; const ll infl = 4e18; template bool chmax(T &a, const T& b) {if (a < b) {a = b; return true;}return false;} template bool chmin(T &a, const T& b) {if (a > b) {a = b; return true;}return false;} template T max(vector &v) {T m = 0;for (auto x : v) chmax(m, x);return m;} template T min(vector &v) {T m = inf;for (auto x : v) chmin(m, x);return m;} template void print(vector &v){for(auto x : v){cout<> H >> W >> N; vpl ab(N); vector> cd(N); rep(i,N){ cin >> ab[i].first >> ab[i].second; get<0>(cd[i])=ab[i].second; get<1>(cd[i])=ab[i].first+ab[i].second; get<2>(cd[i])=i; } sort(cd.begin(),cd.end()); vl dist(N,infl); segtree seg(N); rep(i,N){ seg.set(i,get<1>(cd[i])); } rep(i,N){ int ind=get<2>(cd[i]); ll a=ab[ind].first; ll b=ab[ind].second; chmin(dist[ind],seg.prod(i+1,N)+a-b-2); } rep(i,N){ get<0>(cd[i])=ab[i].second; get<1>(cd[i])=ab[i].first-ab[i].second; get<2>(cd[i])=i; } sort(cd.begin(),cd.end(),greater<>()); rep(i,N){ seg.set(i,get<1>(cd[i])); } rep(i,N){ int ind=get<2>(cd[i]); ll a=ab[ind].first; ll b=ab[ind].second; chmin(dist[ind],seg.prod(i+1,N)+a+b-2); } map mp; rep(i,N){ mp[ab[i].second].push_back({ab[i].first,i}); } for (auto [key,x] : mp){ sort(mp[key].begin(),mp[key].end()); } for(auto [key,x] : mp){ rep(i,x.size()-1){ ll a=x[i].first; ll b=x[i+1].first; ll ind1=x[i].second; ll ind2=x[i+1].second; chmin(dist[ind1],b-a); chmin(dist[ind2],b-a); } } map mp2=mp; for(auto [key,x] : mp2){ for(auto [a,ind] : x){ ll b=key+1; ll c=lower_bound(mp[b].begin(),mp[b].end(),make_pair(a,0LL))-mp[b].begin(); if (c