//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
gpu_hash_table<int, int> mp;
#define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update>
 */
using ll = long long;
using ld = long double;
const ll MOD = 924844033;
const ll root = 62;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
const int mxD = 1001;
int cnt[mxD][mxD];
int psum1[2*mxD][2*mxD];
int psum2[2*mxD][2*mxD];
//bigger to deal with negative shifts
int n, d;
int mxk = 0;
int get1(int x1, int y1, int x2, int y2) {
  return psum1[x2][y2] + psum1[x1][y1] - psum1[x1][y2] - psum1[x2][y1];
}
int get2(int x1, int y1, int x2, int y2) {
  return psum2[x2][y2] + psum2[x1][y1] - psum2[x1][y2] - psum2[x2][y1];
}
bool check(int over) {
  int k = psum1[d][d];
  owo(i, 0, d) {
    owo(j, 0, d) {
      //cout<<over<<" "<<i<<" "<<j<<" "<<get1(i, j, i+over, j+over)<<" "<<get2(i+over, j+over, i+d, j+d)<<"\n";
      if(get1(i, j, i+over, j+over)==k&&get2(i+over, j+over, i+d, j+d)==0)return true;
    }
  }
  //cout<<over<<"\n";
  return false;
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>d;
    int a, b;
    owo(i, 0, n) {
      cin>>a>>b;
      cnt[b%d][a%d]++;
    }
    owo(i, 0, d) {
      owo(j, 0, d) {
        mxk = max(mxk, cnt[i][j]);
      }
    }
    mxk = (int)(sqrt(mxk)-eps);
    owo(i, 0, d) {
      owo(j, 0, d) {
        //cout<<cnt[i][j]<<"\n";
        if(cnt[i][j]>(mxk+1)*(mxk))cnt[i][j] = 0;
        else if(cnt[i][j]>mxk*mxk)cnt[i][j] = 1;
        else cnt[i][j] = 2;
      }
    }
    owo(i, 0, 2*d) {
      owo(j, 0, 2*d) {
        psum1[i+1][j+1] = psum1[i+1][j]; 
        psum2[i+1][j+1] = psum2[i+1][j];
        if(cnt[i%d][j%d]==0) {
          psum1[i+1][j+1]++;
        }
        if(cnt[i%d][j%d]==1||cnt[i%d][j%d]==0) {
          psum2[i+1][j+1]++;
          //cout<<i<<" "<<j<<"\n";
        }
      }
    }
    owo(i, 0, 2*d) {
      owo(j, 0, 2*d) {
        psum1[i+1][j+1] = psum1[i+1][j+1] + psum1[i][j+1];
        psum2[i+1][j+1] = psum2[i+1][j+1] + psum2[i][j+1];
      }
    }
    owo(i, 0, 2*d) owo(j, 0, 2*d) {
      //cout<<i<<" "<<j<<" "<<psum1[i][j]<<" "<<psum2[i][j]<<"\n";
    }
    int l = 0;
    int r = d;
    while(l<r) {
      int mid = l+(r-l)/2;
      if(check(mid))r = mid;
      else l = mid+1;
    }
    cout<<(mxk*d+l-1)<<"\n";
    return 0;
}

Measured memory_bytes: 38808