V_AYP1_E - Secret

Petra Pobre wishes to open a secret room in her school, but the security system consists in a puzzle that is formed by a matrix of size NxM, then, for each column you will have N numbers in any order, to solve the puzzle, you must observe a particular number K that can be found besides the matrix size, to open the door, Petra needs to shout the minimum moves that she needs to make so that every observed number K found in every column is aligned at the first row.

Every column in the matrix can be moved up or down, by instance.

N=4 M=3 K=2;
1 2 3

3 1 2
2 8 4
3 3 5

In the first column the number K moves two units up, in the second column the number is in position and in the last column it goes one unit up, therefore, the result is 3.

 

Input details:

There are three integers to read; N,M and K, everyone corresponding to the number of columns and rows in the matrix with the number to move, the next N lines will contain M integers separated by a single space representing the number in a position of the matrix (i,j)

 

Output details:

You should print the minimum moves so that the condition described previously is satisfied.

 

INPUT

OUTPUT

4 3 2

1 2 3

3 1 2

2 8 4

3 3 5

3

 

Constraints:

0 < N,M < 1500

0 < K < 2000


Added by:Venezuelan Programming League
Date:2013-02-02
Time limit:7s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.