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 <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

ll num[1010][1010];

ll high[2010][2010];
ll middle[2010][2010];
ll low[2010][2010];

ll db[1010];

int main() {
    ll N, D;
    cin >> N >> D;
    for (int i = 0; i < 1010; i++) {
        fill(num[i], num[i] + 1010, 0);
    }
    for (int i = 0; i < 1010; i++) {
        db[i] = i * i;
    }
    for (int i = 0; i < N; i++) {
        ll x, y;
        cin >> x >> y;
        x = x % D;
        y = y % D;
        num[x][y]++;
    }
    ll maxnum = 0;
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < D; j++) {
            maxnum = max(maxnum, num[i][j]);
        }
    }
    ll sz = lower_bound(db, db + 1010, maxnum) - db;
    //cout << sz << endl;
    ll hi = sz * D - 1;
    ll lo = (sz - 1) * D - 1;
    for (int i = 0; i < 2010; i++) {
        fill(high[i], high[i] + 2010, 0);
        fill(middle[i], middle[i] + 2010, 0);
        fill(low[i], low[i] + 2010, 0);
    }
    ll highnum = sz * sz;
    ll middlenum = sz * (sz - 1);
    ll lownum = (sz - 1) * (sz - 1);
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < D; j++) {
            if (num[i][j] > highnum) {
                high[i + 1][j + 1]++;
                high[i + D + 1][j + 1]++;
                high[i + 1][j + D + 1]++;
                high[i + D + 1][j + D + 1]++;
            }
            if (num[i][j] > middlenum) {
                middle[i + 1][j + 1]++;
                middle[i + D + 1][j + 1]++;
                middle[i + 1][j + D + 1]++;
                middle[i + D + 1][j + D + 1]++;
            }
            if (num[i][j] > lownum) {
                low[i + 1][j + 1]++;
                low[i + D + 1][j + 1]++;
                low[i + 1][j + D + 1]++;
                low[i + D + 1][j + D + 1]++;
            }
        }
    }
    for (int i = 0; i < 2 * D + 1; i++) {
        for (int j = 1; j < 2 * D + 1; j++) {
            high[i][j] += high[i][j - 1];
            middle[i][j] += middle[i][j - 1];
            low[i][j] += low[i][j - 1];
        }
    }
    for (int i = 1; i < 2 * D + 1; i++) {
        for (int j = 0; j < 2 * D + 1; j++) {
            high[i][j] += high[i - 1][j];
            middle[i][j] += middle[i - 1][j];
            low[i][j] += low[i - 1][j];
        }
    }
    while (hi - lo > 1) {
        ll mid = (hi + lo) / 2;
        //ll a = mid / D;
        ll b = mid % D + 1;
        bool ok = false;
        for (int i = 0; i < D; i++) {
            for (int j = 0; j < D; j++) {
                // bool subok = true;
                // for (int k = 0; k < D; k++) {
                //     for (int l = 0; l < D; l++) {
                //         ll numkl;
                //         if ((i <= k && k <= i + b) || (i <= k + D && k + D <= i + b)) {
                //             if ((j <= l && l <= j + b) || (j <= l + D && l + D <= j + b)) {
                //                 numkl = (a + 1) * (a + 1);
                //             } else {
                //                 numkl = a * (a + 1);
                //             }
                //         } else {
                //             if ((j <= l && l <= j + b) || (j <= l + D && l + D <= j + b)) {
                //                 numkl = a * (a + 1);
                //             } else {
                //                 numkl = a * a;
                //             }
                //         }
                //         if (num[k][l] > numkl) {
                //             subok = false;
                //             break;
                //         }
                //     }
                //     if (!subok) {
                //         break;
                //     }
                // }
                // if (subok) {
                //     ok = true;
                //     break;
                // }
                ll highover = high[i + b][j + b] - high[i][j + b] - high[i + b][j] + high[i][j];
                ll middleover1 = middle[i + D][j + b] - middle[i + b][j + b]
                    - middle[i + D][j] + middle[i + b][j];
                ll middleover2 = middle[i + b][j + D] - middle[i][j + D]
                    - middle[i + b][j + b] + middle[i][j + b];
                ll lowover = low[i + D][j + D] - low[i + b][j + D]
                    - low[i + D][j + b] + low[i + b][j + b];
                //cout << mid << " " << b;
                //cout << " " << highover << " " << middleover1 << " " << middleover2 << " " << lowover << endl;
                if (highover == 0 && middleover1 == 0 && middleover2 == 0 && lowover == 0) {
                    ok = true;
                    break;
                }
            }
            if (ok) {
                break;
            }
        }
        if (ok) {
            hi = mid;
        } else {
            lo = mid;
        }
    }
    cout << hi << endl;
}