Problem: Problem

Phantom thief Rappan came to steal the jewels. It was easy to get the jewel, but the jewel was equipped with a sensor and was surrounded by security robots.

The guard robot is designed to move towards the jewel. The sensor didn't seem to be easy to remove, so I decided to put a jewel and escape. I decided to keep the jewels as far away from the guard robot as possible to give me time to escape.

The site has a rectangular shape consisting of n x m squares and there are no obstacles. The k-body security robots are arranged in squares (xi, yi) (0 ≤ xi ≤ n−1, 0 ≤ yi ≤ m−1), respectively, and can move to the upper, lower, left and right squares in a unit time. The security robot moves to the square with the jewel in the shortest path.

Find the maximum travel time it takes for one or more guard robots to reach the jeweled square when you are free to place jewels on the premises.

Constraints

* 1 ≤ n, m ≤ 5 × 104
* 1 ≤ k ≤ min (105, n x m)
* 0 ≤ xi ≤ n−1
* 0 ≤ yi ≤ m−1
* All given coordinates are different

Input


n m k
x1 y1
x2 y2
...
xk yk


All inputs are given as integers.
The first line is given n, m, k separated by blanks.
The coordinates (xi, yi) of the square where the guard robot is located are given in the second and subsequent lines k, separated by blanks.

Output

Output the travel time to the place where it takes the longest time for the security robot to reach in one line.

Examples

Input

20 10 1
0 0


Output

28


Input

20 10 2
0 0
17 5


Output

15


Input

20 10 3
0 0
17 5
6 9


Output

11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int,int> P;
typedef pair<P,int> PP;
int h,w,n,N=1<<17;
P a[100000];

class StarrySkyTree{
public:
  int Mi[333333],A[333333];
  void init(){fill(Mi,Mi+333333,0);fill(A,A+333333,0);}
  void add(int a,int b,int x,int k=0,int l=0,int r=N) {
    if(r<=a||b<=l) return;
    if(a<=l&&r<=b){
      A[k]+=x;
      while(k){
        k=(k-1)/2;
        Mi[k]=min(Mi[k*2+1]+A[k*2+1],Mi[k*2+2]+A[k*2+2]);
      }
      return;
    }
    add(a,b,x,k*2+1,l,(l+r)/2);
    add(a,b,x,k*2+2,(l+r)/2,r);
  }
  int getMin(int a,int b,int k=0,int l=0,int r=N) {
    if(r<=a||b<=l)return 1<<29;
    if(a<=l&&r<=b)return Mi[k]+A[k];
    int left=getMin(a,b,k*2+1,l,(l+r)/2),right=getMin(a,b,k*2+2,(l+r)/2,r);
    return min(left,right)+A[k];
  }
};

StarrySkyTree t[2];
vector<PP> v[2][111111];
bool calc(int m) {
  t[0].init(),t[1].init();
  for(int i=0; i<h+w+2; i++) v[0][i].clear(),v[1][i].clear();
  for(int i=0; i<n; i++) {
    int o=(a[i].F+a[i].S)%2;
    int d1=a[i].F+a[i].S-m;
    int d2=a[i].F+a[i].S+m;
    int x=w-(a[i].S-a[i].F+o)/2-1,y=abs(d1)%2;
    d1=max(0,d1),d2=min(h+w,d2);
    x-=(m+y)/2;
    v[0][d1].push_back(PP(P(max(0,x+y),min(h+w,x+m+1)),1));
    v[0][d2+1].push_back(PP(P(max(0,x+y),min(h+w,x+m+1)),-1));
    v[1][d1].push_back(PP(P(max(0,x),min(h+w,x+m+y)),1));
    v[1][d2+1].push_back(PP(P(max(0,x),min(h+w,x+m+y)),-1));
  }
  int z=1,l=w-1,r=w;
  bool f=0,f2=0;
  for(int i=0; i<h+w; i++) {
    for(int k=0; k<2; k++) {
      for(int j=0; j<v[k][i].size(); j++) t[k].add(v[k][i][j].F.F,v[k][i][j].F.S,v[k][i][j].S);
    }
    z=min(z,t[i%2].getMin(l,r));
    if(i>=w-1) f=1;
    if(i>=h-1) f2=1;
    l+=f?(i%2?1:0):(i%2?0:-1);
    r+=f2?(i%2?0:-1):(i%2?1:0);
  }
  return z;
}

int main() {
  cin >> h >> w >> n;
  for(int i=0; i<n; i++) cin >> a[i].F >> a[i].S;
  int l=-1,r=h+w;
  while(l+1<r) {
    int m=(l+r)/2;
    if(calc(m)) r=m;
    else l=m;
  }
  cout << r << endl;
  return 0;
}