Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, an the room located in the i-th row and j-th columns contains the chest of type aij. Each chest of type x ≤ p - 1 contains a key that can open any chest of type x + 1, and all chests of type 1 are not locked. There is exactly one chest of type p and it contains a treasure.

Vanya starts in cell (1, 1) (top left corner). What is the minimum total distance Vanya has to walk in order to get the treasure? Consider the distance between cell (r1, c1) (the cell in the row r1 and column c1) and (r2, c2) is equal to |r1 - r2| + |c1 - c2|.

Input

The first line of the input contains three integers n, m and p (1 ≤ n, m ≤ 300, 1 ≤ p ≤ n·m) — the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.

Each of the following n lines contains m integers aij (1 ≤ aij ≤ p) — the types of the chests in corresponding rooms. It's guaranteed that for each x from 1 to p there is at least one chest of this type (that is, there exists a pair of r and c, such that arc = x). Also, it's guaranteed that there is exactly one chest of type p.

Output

Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type p.

Examples

Input

3 4 3
2 1 1 1
1 1 1 1
2 1 1 3


Output

5


Input

3 3 9
1 3 5
8 9 7
4 6 2


Output

22


Input

3 4 12
1 2 3 4
8 7 6 5
9 10 11 12


Output

11
import java.util.*;
import java.io.*;

/**
  * @author Mighty Cohadar z
  */
public class Delta {

	// E, S, W, N
	static int[] DX = new int[] { +1, 00, -1, 00 };
	static int[] DY = new int[] { 00, +1, 00, -1 };

	final int ny;
	final int nx;
	final int pp;
	final int[][] D;
	final int[][] T;
	final List<List<Integer>> LL;
	final PriorityQueue<Integer> Q;
	
	public Delta(int ny, int nx, int pp, int[][] D, List<List<Integer>> LL) {
		this.ny = ny;
		this.nx = nx;
		this.pp = pp;
		this.D = D;
		this.T = new int[ny][nx];
		this.LL = LL;
		this.Q = new PriorityQueue<>(new Comparator<Integer> () {
			public int compare(Integer a, Integer b) {
				return Integer.compare(T[yy(a)][xx(a)], T[yy(b)][xx(b)]);
			}
		});
	}

	public void solveDirect(List<Integer> A, List<Integer> B) {
		for (Integer b : B) {
			int min = D[yy(b)][xx(b)];
			for (Integer a : A) {
				min = Math.min(min, D[yy(a)][xx(a)] + Math.abs(xx(a) - xx(b)) + Math.abs(yy(a) - yy(b)));
			}
			D[yy(b)][xx(b)] = min;
		}
	}

	public boolean outside(int x, int y) {
		return x < 0 || y < 0 || x >= nx || y >= ny;
	}

	public void solveBFS(List<Integer> A, List<Integer> B) {
		for (int y = 0; y < ny; y++) {
			Arrays.fill(T[y], Integer.MAX_VALUE);
		}
		for (Integer a : A) {
			T[yy(a)][xx(a)] = D[yy(a)][xx(a)];
			Q.add(a);
		}
		while (!Q.isEmpty()) {
			int c = Q.remove(); 
			for (int i = 0; i < 4; i++) {
				int x = xx(c) + DX[i];
				int y = yy(c) + DY[i];
				if (!outside(x, y) && T[y][x] == Integer.MAX_VALUE) {
					T[y][x] = T[yy(c)][xx(c)] + 1;
					Q.add(cell(x, y));
				}
			}
		}
		for (int b : B) {
			D[yy(b)][xx(b)] = T[yy(b)][xx(b)];
		}
	}

	static int xx(int cell) {
		return (cell >> 16) & 0xFFFF;
	}

	static int yy(int cell) {
		return cell & 0xFFFF;
	}

	static int cell(int x, int y) {
		return (x << 16) + y;
	}

	public int solve() {
		for (int i = 1; i < pp; i++) {
			List<Integer> A = LL.get(i-1);
			List<Integer> B = LL.get(i);
			if (A.size() * B.size() <= 10 * ny * nx) {
				solveDirect(A, B);	
			} else {
				solveBFS(A, B);	
			}
		}
		int c = LL.get(pp-1).get(0);
		return D[yy(c)][xx(c)];
	}

	public static Delta load(FastScanner scanner) {
		int ny = scanner.nextInt();
		int nx = scanner.nextInt();
		int pp = scanner.nextInt();
		List<List<Integer>> LL = new ArrayList<>(pp);
		for (int i = 0; i < pp; i++) {
			LL.add(new ArrayList<Integer>());
		}
		int[][] D = new int[ny][nx];
		for (int y = 0; y < ny; y++) {
			for (int x = 0; x < nx; x++) {
				int v = scanner.nextInt() - 1;
				int c = cell(x, y);
				assert xx(c) == x;
				assert yy(c) == y;
				LL.get(v).add(c);
				D[y][x] = (v == 0) ? x + y : Integer.MAX_VALUE;
			}
		}
		return new Delta(ny, nx, pp, D, LL);
	} 

	public static void main(String[] args) {
		FastScanner scanner = new FastScanner(System.in);
		Delta o = Delta.load(scanner);
		System.out.println(o.solve());
	}

}

class FastScanner {
	private final BufferedReader in;
	private final StringBuilder sb;
	private StringTokenizer strtok;
	public FastScanner(InputStream is) {
		this.in = new BufferedReader(new InputStreamReader(is));
		this.sb = new StringBuilder();
		this.strtok = null;
	}
	public String next() {
		try {
			if (strtok == null || strtok.hasMoreTokens() == false) {
				strtok = new StringTokenizer(in.readLine());
			}
			return strtok.nextToken();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public int nextInt() {
		return Integer.parseInt(next());
	}
	public long nextLong() {
		return Long.parseLong(next());
	}
	public String nextLine() {
		try {
			if (strtok == null) {
				return in.readLine();
			} else {
				String ret = (strtok.hasMoreTokens()) ? strtok.nextToken("\n") : "";
				strtok = null;
				return ret;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
