//#define NDEBUG
#include <cstddef>
#include <cstdint>
#include <vector>

using i8 = ::std::int_least8_t;
using i32 = ::std::int_least32_t;
using i64 = ::std::int_least64_t;
using u8 = ::std::uint_least8_t;
using u32 = ::std::uint_least32_t;
using u64 = ::std::uint_least64_t;
using isize = ::std::ptrdiff_t;
using usize = ::std::size_t;

template <class T = usize> class rep {
	const T f, l;

public:
	class itr {
		friend rep;
		T i;
		constexpr itr(T x) noexcept : i(x) {}

	public:
		void operator++() noexcept { ++i; }
		T operator*() const noexcept { return i; }
		bool operator!=(const itr x) const noexcept { return i != x.i; }
	};
	constexpr rep(const T first, const T last) noexcept : f(first), l(last) {}
	itr begin() const noexcept { return itr(f); }
	itr end() const noexcept { return itr(l); }
};
template <class T = usize> class revrep {
	const T f, l;

public:
	class itr {
		friend revrep;
		T i;
		constexpr itr(T x) noexcept : i(x) {}

	public:
		void operator++() noexcept { --i; }
		T operator*() const noexcept { return i; }
		bool operator!=(const itr x) const noexcept { return i != x.i; }
	};
	constexpr revrep(const T first, const T last) noexcept : f(first), l(last) {}
	itr begin() const noexcept { return itr(l - 1); }
	itr end() const noexcept { return itr(f - 1); }
};
template <class T> bool maxi(T &a, const T &b) {
	return a <= b ? (a = b, 1) : 0;
}
template <class T> bool mini(T &a, const T &b) {
	return b <= a ? (a = b, 1) : 0;
}
template <class T> bool smaxi(T &a, const T &b) {
	return b <= a ? 0 : (a = b, 1);
}
template <class T> bool smini(T &a, const T &b) {
	return a <= b ? 0 : (a = b, 1);
}
template <class T> using vec_alias = ::std::vector<T>;
template <class T> auto md_vec(usize n, const T &value) {
	return ::std::vector<T>(n, value);
}
template <class... Args> auto md_vec(usize n, Args... args) {
	return ::std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> const T &as_const(const T &v) noexcept { return v; }

#include <algorithm>
#include <iostream>
#include <utility>

int main() {
	usize n;
	u64 d;
	::std::cin >> n >> d;
	auto sp = md_vec(d, d, u64(0));
	for (const auto i : rep<>(0, n)) {
		u64 x, y;
		::std::cin >> x >> y;
		++sp[x%d][y%d];
	}

	const auto is_ok = [&](const u64 w)->bool {
		const u64 mw = w / d;
		const u64 iw = w % d;
		const u64 aw = d - iw;
		const u64 l0 = mw*mw;
		const u64 l1 = l0 + mw;
		const u64 l2 = l1 + mw + 1;
		auto sum1 = md_vec(d * 2+1, d * 2+1, u64(0)), sum2 = sum1;
		usize c0 = 0, c1 = 0, c2 = 0;
		for (const auto i : rep<>(0, d)) {
			for (const auto j : rep<>(0, d)) {
				if (sp[i][j] > l2) {
					return false;
				}
				else if (sp[i][j] > l1) {
					++c2;
					++sum2[i + d - iw + 1][j + d - iw + 1];
					--sum2[i + d - iw + 1][j + d + 1];
					--sum2[i + d + 1][j + d - iw + 1];
					++sum2[i + d + 1][j + d + 1];
				}
				else if (sp[i][j] > l0) {
					++c1;
					++sum1[i + 1][j + 1];
					--sum1[i + 1][j + aw + 1];
					--sum1[i + aw + 1][j + 1];
					++sum1[i + aw + 1][j + aw + 1];
				}
			}
		}
		for (const auto i : rep<>(0, d * 2)) {
			for (const auto j : rep<>(0, d * 2)) {
				sum1[i + 1][j + 1] +=
					sum1[i + 1][j] + sum1[i][j + 1] - sum1[i][j];
				sum2[i + 1][j + 1] +=
					sum2[i + 1][j] + sum2[i][j + 1] - sum2[i][j];
			}
		}
		for (const auto i : rep<>(0, d)) {
			for (const auto j : rep<>(0, d)) {
				if (sum1[i][j]
					+ sum1[i][j + d]
					+ sum1[i + d][j]
					+ sum1[i + d][j + d] == 0 &&
					sum2[i][j]
					+ sum2[i][j + d]
					+ sum2[i + d][j]
					+ sum2[i + d][j + d] == c2)
					return true;
			}
		}
		return false;
	};

	u64 ng = 0, ok = 1000000;
	while (ok - ng > 1) {
		const u64 mid = (ng + ok) / 2;
		(is_ok(mid) ? ok : ng) = mid;
	}
	::std::cout << ok - 1 << ::std::endl;
	return 0;
}


Measured memory_bytes: 72132