#P1720E. Misha and Paintings

    ID: 7865 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>constructive algorithmsdata structuresgreedyimplementationmath*2700

Misha and Paintings

No submission language available for this problem.

Description

Misha has a square n×nn \times n matrix, where the number in row ii and column jj is equal to ai,ja_{i, j}. Misha wants to modify the matrix to contain exactly kk distinct integers. To achieve this goal, Misha can perform the following operation zero or more times:

  1. choose any square submatrix of the matrix (you choose (x1,y1)(x_1,y_1), (x2,y2)(x_2,y_2), such that x1x2x_1 \leq x_2, y1y2y_1 \leq y_2, x2x1=y2y1x_2 - x_1 = y_2 - y_1, then submatrix is a set of cells with coordinates (x,y)(x, y), such that x1xx2x_1 \leq x \leq x_2, y1yy2y_1 \leq y \leq y_2),
  2. choose an integer kk, where 1kn21 \leq k \leq n^2,
  3. replace all integers in the submatrix with kk.

Please find the minimum number of operations that Misha needs to achieve his goal.

The first input line contains two integers nn and kk (1n500,1kn21 \leq n \leq 500, 1 \leq k \leq n^2)  — the size of the matrix and the desired amount of distinct elements in the matrix.

Then nn lines follows. The ii-th of them contains nn integers ai,1,ai,2,,ai,na_{i, 1}, a_{i, 2}, \ldots, a_{i, n} (1ai,jn21 \leq a_{i,j} \leq n^2) — the elements of the ii-th row of the matrix.

Output one integer — the minimum number of operations required.

Input

The first input line contains two integers nn and kk (1n500,1kn21 \leq n \leq 500, 1 \leq k \leq n^2)  — the size of the matrix and the desired amount of distinct elements in the matrix.

Then nn lines follows. The ii-th of them contains nn integers ai,1,ai,2,,ai,na_{i, 1}, a_{i, 2}, \ldots, a_{i, n} (1ai,jn21 \leq a_{i,j} \leq n^2) — the elements of the ii-th row of the matrix.

Output

Output one integer — the minimum number of operations required.

Samples

Sample Input 1

3 4
1 1 1
1 1 2
3 4 5

Sample Output 1

1

Sample Input 2

3 2
2 1 3
2 1 1
3 1 2

Sample Output 2

2

Sample Input 3

3 3
1 1 1
1 1 2
2 2 2

Sample Output 3

1

Sample Input 4

3 2
1 1 1
1 2 1
2 2 2

Sample Output 4

0

Note

In the first test case the answer is 11, because one can change the value in the bottom right corner of the matrix to 11. The resulting matrix can be found below:

111
112
341

In the second test case the answer is 22. First, one can change the entire matrix to contain only 11s, and the change the value of any single cell to 22. One of the possible resulting matrices is displayed below:

111
111
112