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<cstdio>
#include<vector>
int x[100005], y[100005];
std::vector<int>need[1005];
int fixX[1005], fixY[1005];
int tag[1005];
long long pair[1005][1005];

long long getNumber(long long l,long long r,long long d,long long left){
    if(l%d<=left) l = l-l%d+left;
    else l = l+d-l%d+left;
    return (r-l+d)/d;
}

int isValid(long long lx,long long rx,long long ly,long long ry,long long d){
    for(int i = 0; i < d; i++){
        for(int j = 0; j < d; j++){
            long long have = getNumber(lx,rx,d,i)*getNumber(ly,ry,d,j);
            //printf("i = %d, j = %d: %lld\n",i,j,have);
            if(have<pair[i][j]) return 0;
        }
    }
    return 1;
}
//length that can't cover 
int isGood(int len,int d){
    int find = 0;
    for(int i = 0; i<d; i++){
        int ok = 1;
        for(int j = 0; j < d; j++){
            if(fixY[j]!=-1) tag[j] = 1;
            else tag[j] = 0;
        }
        for(int j = 0; j < len; j++){ 
            int p = (j+i)%d;
            if(fixX[p]!=-1) ok = 0;
            if(need[p].size()!=0){ 
                for(int k = 0; k < (int)need[p].size(); k++) tag[need[p][k]] = 1;
            }
        }
        if(!ok) continue;
        else{
            int sum = 0;
            for(int j = 0; j < len; j++) sum += tag[j];
            if(sum==0) find = 1;
            //printf("sum = %d\n",sum);
            for(int j = 1; j<d; j++){
                sum -= tag[j-1];
                int p = (j+len-1)%d;
                sum += tag[p];
                if(sum==0){ 
                    //printf("i = %d, j = %d\n",i,j);
                    find = 1;
                }
            }
        }
    }
    return find;
}

int main(){
    int n,d;
    scanf("%d%d",&n,&d);
    for(int i = 1; i <= n; i++){ 
        scanf("%d%d",&x[i],&y[i]);
        pair[x[i]%d][y[i]%d]++;
    }
    /*for(int i = 0; i < d; i++){
        for(int j = 0; j < d; j++) printf("pair[%d][%d] = %lld\n",i,j,pair[i][j]);
    }*/
    long long L = 1, R = n*d;
    long long res = -1;
    while(L<=R){
        long long M = (L+R)/2;
        long long cnt = (M+1)/d;
        int ok = 1;
        for(int i = 0; i < d; i++) fixX[i] = fixY[i] = -1, need[i].clear();
        for(int i = 0; i < d; i++){
            for(int j = 0; j < d; j++){
                if(pair[i][j]>cnt*cnt){
                    if(pair[i][j]<=cnt*(cnt+1ll)){ 
                        need[i].push_back(j);
                    }
                    else if(pair[i][j]<=(cnt+1ll)*(cnt+1ll)) fixX[i] = fixY[j] = 1;
                    else ok = 0;
                }
            }
        }
        if(!ok) L = M+1;
        else{
            //for(int i = 0; i < d; i++) printf("fx[%d] = %d, fy[%d] = %d\n",i,fixX[i],i,fixY[i]);
            int len = d-(M+1)%d;
            //printf("len = %d\n",len);
            if(isGood(len,d)){
                res = M;
                R = M-1;
            }
            else L = M+1;
        }
    }
    printf("%lld\n",res);
    return 0;
}