Problem:
Score : 800 points

Problem Statement

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

Partial Scores

500 points will be awarded for passing the test set satisfying 1 \leq D \leq 30.

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.

Sample Input 1

3 1
0 0
1 0
2 0

Sample Output 1

1

Sample Input 2

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

Sample Output 2

4

Sample Input 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

// vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
#define min(x,y) (x < y ? x : y)
#define max(x,y) (x < y ? y : x)
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

void solve() {
  ll n, D; cin >> n >> D;
  // assert(D <= 30);
  vector<vector<ll>> sz(D, vector<ll>(D, 0));
  rep(i, n) {
    ll x, y; cin >> x >> y;
    sz[y % D][x % D]++;
  }
  vector<ll> szX(D, 0), szY(D, 0);
  vector<ll> minSzX(D, linf), minSzY(D, linf);
  vector<ll> maxSzX(D, 0), maxSzY(D, 0);
  rep(y, D) rep(x, D) {
    szX[x] += sz[y][x];
    szY[y] += sz[y][x];
    chmin(minSzX[x], sz[y][x]);
    chmin(minSzY[y], sz[y][x]);
    chmax(maxSzX[x], sz[y][x]);
    chmax(maxSzY[y], sz[y][x]);
  }
  auto check = [&](ll L) {
    vector<vector<ll>> sum(D, vector<ll>(D, 0));
    rep(y, D) rep(x, D) {
      sum[y][x] = ((L - y + D - 1) / D) * ((L - x + D - 1) / D);
    }
    // cout << sum << endl;
    // cout << endl;
    vector<ll> sumX(D, 0), sumY(D, 0);
    vector<ll> minX(D, linf), minY(D, linf);
    vector<ll> maxX(D, 0), maxY(D, 0);
    rep(y, D) rep(x, D) {
      sumX[x] += sum[y][x];
      sumY[y] += sum[y][x];
      chmin(minX[x], sum[y][x]);
      chmin(minY[y], sum[y][x]);
      chmax(maxX[x], sum[y][x]);
      chmax(maxY[y], sum[y][x]);
    }
    vector<ll> X, Y;
    rep(by, D) {
      bool f = true;
      rep(y, D) {
        if (szY[(by+y)%D] > sumY[y] || maxY[y] < minSzY[(by+y)%D]) {
          f = false;
          break;
        }
      }
      if (f) Y.pb(by);
    }
    rep(bx, D) {
      bool f = true;
      rep(x, D) {
        if (szX[(bx+x)%D] > sumX[x] || maxX[x] < minSzX[(bx+x)%D]) {
          f = false;
          break;
        }
      }
      if (f) X.pb(bx);
    }
    bool fx = false, fy = false;
    rep(by, D) {
      bool f = true;
      rep(y, D) {
        if (minY[y] < maxSzY[(by+y)%D]) {
          f = false;
          break;
        }
      }
      if (f) fy = true;
    }
    rep(bx, D) {
      bool f = true;
      rep(x, D) {
        if (minY[x] < maxSzX[(bx+x)%D]) {
          f = false;
          break;
        }
      }
      if (f) fx = true;
    }
    if (fx && fy) return true;
    each(bx, X) each(by, Y) {
      bool f = true;
      rep(y, D) {
        rep(x, D) {
          if (sz[(by+y)%D][(bx+x)%D] > sum[y%D][x%D]) {
            f = false;
            break;
          }
        }
        if (!f) break;
      }
      if (f) {
        return true;
      }
    }
    return false;
  };
  ll lb = 0, ub = ll(1e9);
  rep(t, 60) {
    ll mid = (lb + ub) / 2;
    if (check(mid)) ub = mid;
    else lb = mid;
  }
  // cout << lb << " " << ub << endl;
  cout << ub-1 << endl;
}