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;

// when range [l, r), has property notP~P, want first P.
// when return r, means not found.
template <typename T>
T bs_first(T l, T r, function<bool (T)> f) {
    assert(l < r);
    T mid;
    while (l != r) {
        mid = l + (r-l)/2;
        if (f(mid)) {
            r = mid;
        }else {
            l = mid + 1;
        }
    }
    return r;
}

const int N = 1024; 
int cnt[N][N];
int a[N<<1][N<<1];
int b[N<<1][N<<1];

void solve() {
    int n, D;
    cin >> n >> D;
    for (int _ = 0; _ < n; _++) {
        int x,y;
        cin >> x >> y;
        x%=D;
        y%=D;
        cnt[x][y]++;
    }
    n = 0;
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < D; j++) {
            int k = 0;
            while (k*k < cnt[i][j]) {
                k++;
            }
            n = max(n,k);
        }
    }
    int z = (n-1)*n;
    int p = (n-1)*(n-1);
    int Z = 0;
    int P = 0;
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < D; j++) {
            if (cnt[i][j] > z) {
                Z++;
                int x=i+1, y=j+1;
                a[x][y] = a[x][y+D] = a[x+D][y] = a[x+D][y+D] = 1;
            } else if (cnt[i][j] > p) {
                P++;
                int x=i+1, y=j+1;
                b[x][y] = b[x][y+D] = b[x+D][y] = b[x+D][y+D] = 1;
            }
        }
    }
    const int LIM = D<<1; 
    for (int i = 1; i <= LIM; i++) {
        for (int j = 1; j <= LIM; j++) {
            a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
            b[i][j] += b[i-1][j] + b[i][j-1] - b[i-1][j-1];
        }
    }
    auto sum = [&](int i, int j, int d){
        return a[i+d][j+d] - a[i][j+d] - a[i+d][j] + a[i][j];
    };
    auto sum_b = [&](int i, int j, int d){
        return b[i+d][j+d] - b[i][j+d] - b[i+d][j] + b[i][j];
    };
    auto cover = [&](int i, int j, int d){
        return P - sum_b(i+d,j+d,D-d);
    };

    if (D == 1) {
        cout << (n-1); return;
    }

    int res = D;
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < D; j++) {
            int tmp = bs_first<int>(1, D, [&](int d){
                    return sum(i,j,d) >= Z;
                    });
            int cov = bs_first<int>(1, D, [&](int d){
                    return cover(i,j,d) >= P;
                    });
            tmp = max(tmp, cov);
            res = min(res, tmp);
        }
    }
    res += (n-1)*D - 1;
    cout << res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}