#include <bits/stdc++.h>
using namespace std;
const int N = 1006;
int n, d;
int mat[N][N];
int lx[N], ly[N];
bool fits(int x, int y, int qnt, long long side) {
  long long tmp = (side - x) / d + 1;
  tmp *= (side - y) / d + 1;
  return tmp >= qnt;
}
int main() {
  scanf("%d %d", &n, &d);
  for (int i = 0, x, y; i < n; i++) {
    scanf("%d %d", &x, &y);
    mat[x % d][y % d]++;
  }
  memset(lx, -1, sizeof lx);
  memset(ly, -1, sizeof ly);
  for (int i = 0; i < d; i++)
    for (int j = 0; j < d; j++) {
      if (mat[i][j]) {
        lx[(i + 1) % d] = i;
        ly[(j + 1) % d] = j;
      }
    }
  for (int i = 0; i < 5 * d; i++)
    if (lx[i % d] == -1) {
      if (lx[(i + d - 1) % d] != -1) lx[i % d] = lx[(i + d - 1) % d];
    }
  for (int i = 0; i < 5 * d; i++)
    if (ly[i % d] == -1) {
      if (ly[(i + d - 1) % d] != -1) ly[i % d] = ly[(i + d - 1) % d];
    }
  long long ans = 1e18;
  for (int x = 0; x < d; x++)
    for (int y = 0; y < d; y++) {
      long long now = 0;
      now = max((lx[x] - x + d) % d, (ly[y] - y + d) % d);
      for (int i = 0; i < d; i++)
        for (int j = 0; j < d; j++) {
          int a = (i - x + d) % d, b = (j - y + d) % d;
          while (!fits(a, b, mat[i][j], now)) now++;
        }
      ans = min(ans, now);
      break;
    }
  printf("%lld\n", ans);
}
