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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int cnt[1010][1010];

int get(vector<vector<int>> &v,int x1,int y1,int x2,int y2){
    return v[x2][y2]+v[x1][y1]-v[x2][y1]-v[x1][y2];
}

int main(){
    int n,d;
    cin>>n>>d;
    rep(i,n){
        int x,y;
        cin>>x>>y;
        cnt[x%d][y%d]++;
    }
    int ma=0;
    rep(i,d)rep(j,d){
        int k=1;
        int rt=sqrt(cnt[i][j]);
        if(rt*rt<cnt[i][j])++rt;
        ma=max(rt,ma);
    }

    vector<vector<int>> sum1(2*d+2,vector<int>(2*d+2)),sum2(2*d+2,vector<int>(2*d+2));
    int tot1=0,tot2=0;
    rep(i,d)rep(j,d){
        if(cnt[i][j]>ma*(ma-1)){
            ++sum1[i][j],
            ++sum1[i+d][j],
            ++sum1[i][j+d],
            ++sum1[i+d][j+d],
            ++tot1;
        }
        else if(cnt[i][j]>(ma-1)*(ma-1)){
            ++sum2[i][j],
            ++sum2[i+d][j],
            ++sum2[i][j+d],
            ++sum2[i+d][j+d],
            ++tot2;
        }
    }
    rep(i,2*d)rep(j,2*d){
        sum1[i+1][j]+=sum1[i][j];
        sum2[i+1][j]+=sum2[i][j];
    }
    rep(i,2*d)rep(j,2*d){
        sum1[i][j+1]+=sum1[i][j];
        sum2[i][j+1]+=sum2[i][j];
    }

    int ok=d,ng=-1,mid;
    while(ok-ng>1){
        mid=(ok+ng)/2;
        bool can=false;
        rep(i,d)rep(j,d){
            if(get(sum1,i,j,i+mid,j+mid)==tot1&&
            get(sum2,i+mid,j+mid,i+d,j+d)==0){
                can=true;
                break;
            }
        }
        if(can)ok=mid;
        else ng=mid;
    }

    cout<<(ma-1)*d+ok-1 <<endl;
}