Problem: Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.

Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.

In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).

He may perform the following operation arbitrarily many times:

* Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.



Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.

Find the minimum scatteredness after he performs arbitrarily many operations.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq D \leq 1000
* 0 \leq x_i, y_i \leq 10^9
* Given coordinates are pairwise distinct
* All numbers given in input are integers

Input

Input is given from Standard Input in the following format:


N D
x_1 y_1
:
x_N y_N


Output

Print the answer.

Examples

Input

3 1
0 0
1 0
2 0


Output

1


Input

19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0


Output

4


Input

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5


Output

4

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.first<<","<<p.second<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
constexpr int SIZE = 100;
#else
#define dbg(...) {}
constexpr int SIZE = 4096;
#endif

int val[2][2005][2005];

int main(){
  int n,d;
  cin>>n>>d;
  vector<vector<int>> v(d,vector<int>(d,0));
  rep(i,n){
    int x,y;
    cin>>x>>y;
    v[x%d][y%d]++;
  }

  long l = 1, r = n*d;
  while(r-l > 1) {
    long mid = (l+r)/2;
    auto f = [&](){
      if (mid * mid < n) return false;
      if (mid%d==0){
        rep(i,d)rep(j,d) if(v[i][j] > (mid/d) * (mid/d)) return false;
        return true;
      }
      rep(i,d) rep(j,d) if(v[i][j] > (mid/d+1) * (mid/d+1)) return false;

      fill(val[0][0], val[2][0], 0);
      rep(i,1,d+1) rep(j,1,d+1){
        val[0][i][j] = val[0][i+d][j] = val[0][i][j+d] = val[0][i+d][j+d] = v[i-1][j-1] <= (mid/d + 1)*(mid/d);
        val[1][i][j] = val[1][i+d][j] = val[1][i][j+d] = val[1][i+d][j+d] = v[i-1][j-1] <= (mid/d)*(mid/d);
      }
      rep(k,2){
        rep(i,2*d+1){
          rep(j,2*d+1){
            if(i>0) val[k][i][j] += val[k][i-1][j];
            if(j>0) val[k][i][j] += val[k][i][j-1];
            if(i>0 && j>0) val[k][i][j] -= val[k][i-1][j-1];
          }
        }
      }

      rep(i,d) rep(j,d){
        if(val[1][i + d][j + d] - val[1][i + d][j + mid%d] - val[1][i + mid%d][j + d] + val[1][i + mid%d][j + mid%d] != (d - mid%d)*(d - mid%d)) continue;
        if(val[0][i + mid%d][j + d] - val[0][i][j + d] - val[0][i + mid%d][j + mid%d] + val[0][i][j + mid%d] != (d - mid%d)*(mid%d)) continue;
        if(val[0][i + d][j + mid%d] - val[0][i + d][j] - val[0][i + mid%d][j + mid%d] + val[0][i + mid%d][j] != (d - mid%d)*(mid%d)) continue;
        return true;
      }
      return false;
    };
    if (f()) r = mid;
    else l = mid;
  }

  cout << l << endl;

  return 0;
}