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 <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;

int a[1000][1000];
int ss0[2005][2005];
int ss[2005][2005];

int main(int argc, char* argv[])
{
    int n,D;
    scanf("%d%d", &n, &D);

    int i,j;
    for(i=0; i<n; i++) {
        int x,y;
        scanf("%d%d", &x, &y);
        a[x%D][y%D]++;
    }

    int max=0;
    for(i=0; i<D; i++) {
        for(j=0; j<D; j++) {
            max=MAX(max,a[i][j]);
        }
    }
    double tmp=sqrt((double)max);
    int mm=(int)tmp;
    if(mm*mm<max) {
        mm++;
    }
    assert(mm*mm>=max);
    mm--;
    //printf("%d\n", mm);

    for(i=0; i<D; i++) {
        for(j=0; j<D; j++) {
            if(a[i][j]<=mm*mm) {
                a[i][j]=0;
            }
            else if(a[i][j]<=mm*(mm+1)) {
                a[i][j]=1;
            }
            else {
                assert(a[i][j]<=(mm+1)*(mm+1));
                a[i][j]=2;
            }
        }
    }

    for(i=0; i<D*2; i++) {
        for(j=0; j<D*2; j++) {
            ss0[i][j+1]=ss0[i][j]+a[i%D][j%D];
        }
    }
    for(j=0; j<=D*2; j++) {
        for(i=0; i<D*2; i++) {
            ss[i+1][j]=ss[i][j]+ss0[i][j];
        }
    }

    vector<int> max0(D*2),max1(D*2);
    for(i=0; i<D; i++) {
        for(j=0; j<D; j++) {
            max0[i]=MAX(max0[i],a[i][j]-1);
            max1[j]=MAX(max1[j],a[i][j]-1);
        }
    }
    for(i=0; i<D; i++) {
        max0[i+D]=max0[i];
        max1[i+D]=max1[i];
    }
    vector<int> smax0(D*2+1),smax1(D*2+1);
    for(i=0; i<D*2; i++) {
        smax0[i+1]=smax0[i]+max0[i];
        smax1[i+1]=smax1[i]+max1[i];
    }

    ll ans=LINF;
    for(i=0; i<D; i++) {
        for(j=0; j<D; j++) {
            int l=0, r=D;
            while(r-l>1) {
                int m=(l+r)/2;
                int tmp0=smax0[i+D]-smax0[i+m];
                int tmp1=smax1[j+D]-smax1[j+m];
                int tmp2=ss[i+D][j+D]-ss[i+m][j+D]-ss[i+D][j+m]+ss[i+m][j+m];
                if(tmp0==0 && tmp1==0 && tmp2==0) {
                    r=m;
                }
                else {
                    l=m;
                }
            }
            ans=MIN(ans,D*mm+l);
        }
    }
    printf("%lld\n", ans);

    return 0;
}