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;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
#define rep(i, N) for(int i = 0; i < int(N); i++)
#define rep1(i, N) for (int i = 1; i <= int(N); i++)
#define fs first
#define sc second
#define pb push_back
#define PB push_back
#define MP make_pair
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)

V<int> slide_max(const V<int>& v, int k) {
    V<int> idx; int st = 0;
    auto add = [&](int i) {
        while (st != int(idx.size()) && v[idx.back()] <= v[i]) idx.pop_back();
        idx.push_back(i);
    };

    int n = int(v.size());
    for (int i = 0; i < k-1; i++) {
        add(i);
    }
    V<int> res(n - k + 1);
    for (int i = 0; i <= n - k; i++) {
        add(i + (k-1));
        res[i] = v[idx[st]];
        if (idx[st] == i) st++;
    }
    return res;
}

int n;
int g[2010][2010];

VV<int> slide_max2d(int r, int c) {
    int h = n, w = n;

    static int hol[2010][2010];
    //VV<int> hol(w);
    for (int i = 0; i < w; i++) {
        V<int> buf(h);
        for (int j = 0; j < h; j++) {
            buf[j] = g[j][i];
        }
        auto res = slide_max(buf, r);
        for (int j = 0; j <= h - r; j++) {
            hol[i][j] = res[j];
        }
//        hol[i] = slide_max(buf, r);
    }
    VV<int> res(h - r + 1);
    for (int i = 0; i <= h - r; i++) {
        V<int> buf(w);
        for (int j = 0; j < w; j++) {
            buf[j] = hol[j][i];
        }
        res[i] = slide_max(buf, c);
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int m, d;
    cin >> m >> d;
    n = 2*d;

    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        g[x%d][y%d]++;
        g[x%d+d][y%d]++;
        g[x%d][y%d+d]++;
        g[x%d+d][y%d+d]++;
    }

    int ma = 0;
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < d; j++) {
            ma = max(ma, g[i][j]);
        }
    }



/*    for (int i = 0; i < d; i++) {
        for (int j = 0; j < d; j++) {
            cout << g[i][j] << " ";
        }
        cout << endl;
    }*/
    ll lw = max(0LL, ll(sqrt(ma) - 1 + 1e-10) * d), up = ll(ceil(sqrt(ma) - 1e-10)) * d;
    while (up - lw > 1) {
        ll md = (lw + up) / 2;
        ll l = md / d;
        int rem = md % d;

        bool ok = false;
        auto big = slide_max2d(d - rem, d - rem);
        if (rem == 0) {
            for (int x = 0; x < d; x++) {
                for (int y = 0; y < d; y++) {
                    if (big[x][y] > l*l) continue;
                    ok = true;
                    break;
                }
                if (ok) break;
            }
        } else {
            auto small = slide_max2d(rem, rem);
            auto hbig = slide_max2d(d - rem, rem);
            auto wbig = slide_max2d(rem, d - rem);

            for (int x = 0; x < d; x++) {
                for (int y = 0; y < d; y++) {
                    if (big[x + rem][y + rem] > l * l) continue;
                    if (small[x][y] > l * l + l + l + 1) continue;
                    if (hbig[x + rem][y] > l * l + l) continue;
                    if (wbig[x][y + rem] > l * l + l) continue;
                    ok = true;
                    break;
                }
                if (ok) break;
            }
        }
        if (!ok) {
            lw = md;
        } else {
            up = md;
        }
    }
    cout << lw << endl;
    return 0;
}