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:
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
gpu_hash_table<int, int> mp;
#define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update>
 */
using ll = long long;
using ld = long double;
const ll MOD = 924844033;
const ll root = 62;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
const int mxD = 1001;
int cnt[mxD][mxD];
int psum1[2*mxD][2*mxD];
int psum2[2*mxD][2*mxD];
//bigger to deal with negative shifts
int n, d;
int mxk = 0;
int get1(int x1, int y1, int x2, int y2) {
  return psum1[x2][y2] + psum1[x1][y1] - psum1[x1][y2] - psum1[x2][y1];
}
int get2(int x1, int y1, int x2, int y2) {
  return psum2[x2][y2] + psum2[x1][y1] - psum2[x1][y2] - psum2[x2][y1];
}
bool check(int over) {
  int k = psum1[d][d];
  owo(i, 0, d) {
    owo(j, 0, d) {
      //cout<<over<<" "<<i<<" "<<j<<" "<<get1(i, j, i+over, j+over)<<" "<<get2(i+over, j+over, i+d, j+d)<<"\n";
      if(get1(i, j, i+over, j+over)==k&&get2(i+over, j+over, i+d, j+d)==0)return true;
    }
  }
  //cout<<over<<"\n";
  return false;
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>d;
    int a, b;
    owo(i, 0, n) {
      cin>>a>>b;
      cnt[b%d][a%d]++;
    }
    owo(i, 0, d) {
      owo(j, 0, d) {
        mxk = max(mxk, cnt[i][j]);
      }
    }
    mxk = (int)(sqrt(mxk)-eps);
    owo(i, 0, d) {
      owo(j, 0, d) {
        //cout<<cnt[i][j]<<"\n";
        if(cnt[i][j]>(mxk+1)*(mxk))cnt[i][j] = 0;
        else if(cnt[i][j]>mxk*mxk)cnt[i][j] = 1;
        else cnt[i][j] = 2;
      }
    }
    owo(i, 0, 2*d) {
      owo(j, 0, 2*d) {
        psum1[i+1][j+1] = psum1[i+1][j]; 
        psum2[i+1][j+1] = psum2[i+1][j];
        if(cnt[i%d][j%d]==0) {
          psum1[i+1][j+1]++;
        }
        if(cnt[i%d][j%d]==1||cnt[i%d][j%d]==0) {
          psum2[i+1][j+1]++;
          //cout<<i<<" "<<j<<"\n";
        }
      }
    }
    owo(i, 0, 2*d) {
      owo(j, 0, 2*d) {
        psum1[i+1][j+1] = psum1[i+1][j+1] + psum1[i][j+1];
        psum2[i+1][j+1] = psum2[i+1][j+1] + psum2[i][j+1];
      }
    }
    owo(i, 0, 2*d) owo(j, 0, 2*d) {
      //cout<<i<<" "<<j<<" "<<psum1[i][j]<<" "<<psum2[i][j]<<"\n";
    }
    int l = 0;
    int r = d;
    while(l<r) {
      int mid = l+(r-l)/2;
      if(check(mid))r = mid;
      else l = mid+1;
    }
    cout<<(mxk*d+l-1)<<"\n";
    return 0;
}