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 Fortran (gfortran v4.8.4) (judge verdict: Accepted)

module mod_square_rotation
  implicit none
  integer :: d, s(3,0:2000,0:2000)
contains
  subroutine cumsum(a,k)
    integer, intent(in) :: a(0:d-1,0:d-1), k
    integer :: i, j, l, b(3,0:d-1,0:d-1)
    b = 0
    do i = 0, d-1
      do j = 0, d-1
        if (a(i,j) > (k+1)*(k+1)) b(1,i,j) = b(1,i,j)+1
        if (a(i,j) > k*k) b(2,i,j) = b(2,i,j)+1
        if (a(i,j) > k*(k+1)) b(3,i,j) = b(3,i,j)+1
      end do
    end do
    s = 0
    do i = 1, 2*d
      do j = 1, 2*d
        s(:,i,j) = s(:,i-1,j)+s(:,i,j-1)-s(:,i-1,j-1) &
        & +b(:,mod(i-1,d),mod(j-1,d))
      end do
    end do
  end
  integer function getsum(l,i1,j1,i2,j2)
    integer, intent(in) :: l, i1, j1, i2, j2
    getsum = s(l,i2,j2)-s(l,i1-1,j2)-s(l,i2,j1-1)+s(l,i1-1,j1-1)
  end
  logical function check(b)
    integer, intent(in) :: b
    integer :: i, j
    check = .false.
    do i = 1, d
      do j = 1, d
        if (getsum(1,i,j,i+b,j+b) /= 0) cycle
        if (getsum(2,i+b+1,j+b+1,i+d-1,j+d-1) /= 0) cycle
        if (getsum(3,i+b+1,j,i+d-1,j+b) /= 0) cycle
        if (getsum(3,i,j+b+1,i+b,j+d-1) /= 0) cycle
        check = .true.
        return
      end do
    end do
  end
end module mod_square_rotation
program square_rotation
  use mod_square_rotation
  implicit none
  integer :: n, x, y, a(0:1000,0:1000) = 0, i, j, m, k, l, r
  read(*,*) n, d
  do i = 1, n
    read(*,*) x, y
    x = mod(x,d)
    y = mod(y,d)
    a(x,y) = a(x,y)+1
  end do
  m = maxval(a(0:d-1,0:d-1))
  k = 0
  do while (m > (k+1)*(k+1))
    k = k+1
  end do
  call cumsum(a(0:d-1,0:d-1),k)
  l = -1
  r = d-1
  do while (r-l > 1)
    m = (l+r)/2
    if (check(m)) then
      r = m
    else
      l = m
    end if
  end do
  write(*,'(i0)') k*d+r
end program square_rotation