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 static java.lang.Math.*;
import static java.util.Arrays.*;
import java.util.*;
import java.io.*;

public class Main {

	int[][] a;
	IntVec[] ps;
	int[][] dp;
	static int INF = 1001001001;
	int[] bfs_q;
	int[] dr = {0, 1, 0, -1};
	int[] dc = {1, 0, -1, 0};
	int[][] bfs_dist;
	
	void solve() {
		int N = sc.nextInt();
		int M = sc.nextInt();
		int P = sc.nextInt();
		ps = new IntVec[P+1];
		for (int i = 1; i <= P; i++) ps[i] = new IntVec(10);
		
		a = new int[N][M];
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				int p = sc.nextInt();
				a[i][j] = p;
				ps[p].add((i << 10) + j);
			}
		}
		dp = new int[N][M];
		for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) dp[i][j] = INF;
		bfs_q = new int[N * M];
		bfs_dist = new int[N][M];
		
		for (int i = 0; i < ps[1].size(); i++) {
			int pos = ps[1].get(i);
			int r = pos >> 10;
			int c = pos & 1023;
			dp[r][c] = r + c; 
		}
		
		for (int p = 1; p < P; p++) {
			if (ps[p].size() * ps[p+1].size <= N*M) {
//				tr(p, "dp");
				for (int i = 0; i < ps[p].size; i++) {
					int r1 = ps[p].get(i) >> 10;
					int c1 = ps[p].get(i) & 1023;
					for (int j = 0; j < ps[p+1].size; j++) {
						int r2 = ps[p+1].get(j) >> 10;
						int c2 = ps[p+1].get(j) & 1023;
						int d = abs(r1 - r2) + abs(c1 - c2);
						dp[r2][c2] = min(dp[r2][c2], dp[r1][c1] + d);
					}
				}
			} else {
//				tr(p, "dfs");
				dfs(N, M, p);
			}
//			for (int i = 0; i < N; i++) tr(dp[i]);
		}
		int ans = dp[ps[P].front()>>10][ps[P].front()&1023];
		out.println(ans);
	}
	
	void dfs(int N, int M, int p) {
		
		for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) 
			bfs_dist[i][j] = INF;
		
		List<State> initList = new ArrayList<>();
		for (int i = 0; i < ps[p].size(); i++) {
			int pos = ps[p].get(i);
			int r = pos >> 10;
			int c = pos & 1023;
			int cost = dp[r][c];
			initList.add(new State(cost, pos));
			bfs_dist[r][c] = cost;
		}
		
		Collections.sort(initList);
		int initListIndex = 0;
		int tail = 0, head = 0;
		
		bfs_q[head++] = initList.get(initListIndex).pos;
		bfs_dist[initList.get(initListIndex).pos >> 10][initList.get(initListIndex).pos & 1023] = initList.get(initListIndex).cost;
		initListIndex++;
		
		while (tail < head) {
			int pos = bfs_q[tail++];
			int r = pos >> 10;
			int c = pos & 1023;
			while (initListIndex < initList.size() && initList.get(initListIndex).cost <= bfs_dist[r][c] + 1) {
				bfs_q[head++] = initList.get(initListIndex).pos;
				bfs_dist[initList.get(initListIndex).pos >> 10][initList.get(initListIndex).pos & 1023] = initList.get(initListIndex).cost;
				initListIndex++;
			}
			for (int i = 0; i < 4; i++) {
				int nr = r + dr[i];
				int nc = c + dc[i];
				if (0 <= nr && nr < N && 0 <= nc && nc < M) {
					if (bfs_dist[nr][nc] > bfs_dist[r][c] + 1) {
						bfs_dist[nr][nc] = bfs_dist[r][c] + 1;
						bfs_q[head++] = (nr << 10) + nc;
					}
				}
			}
		}
		
		for (int i = 0; i < ps[p+1].size(); i++) {
			int pos = ps[p+1].get(i);
			int r = pos >> 10;
			int c = pos & 1023;
			dp[r][c] = bfs_dist[r][c];
		}
		
	}
	
	static class State implements Comparable<State> {
		int cost;
		int pos;
		public State(int cost, int pos) {
			this.cost = cost;
			this.pos = pos;
		}
		
		@Override
		public int compareTo(State o) {
			return Integer.compare(this.cost, o.cost);
		}
	}

	static class IntVec {
		private int size;
		private int[] data;
		IntVec(int maxSize) {
			size = 0;
			data = new int[maxSize];
		}
		int size() { return size; }
		int get(int index) { return data[index]; }
		void add(int a) {
			if (size >= data.length) {
				int[] newData = new int[(data.length * 3) >> 1];
				System.arraycopy(data, 0, newData, 0, data.length);
				data = newData;
			}
			data[size++] = a;
		}
		int front() { return data[0]; }
		int back() { return data[size-1]; }
		void clear() { size = 0; }
	}

	
	static void tr(Object... os) { System.err.println(deepToString(os)); }
	static void tr(int[][] as) { for (int[] a : as) tr(a); }

	void print(int[] a) {
		out.print(a[0]);
		for (int i = 1; i < a.length; i++) out.print(" " + a[i]);
		out.println();
	}

	public static void main(String[] args) throws Exception {
		new Main().run();
	}

	MyScanner sc = null;
	PrintWriter out = null;
	public void run() throws Exception {
		sc = new MyScanner(System.in);
		out = new PrintWriter(System.out);
		for (;sc.hasNext();) {
			solve();
			out.flush();
		}
		out.close();
	}

	class MyScanner {
		String line;
		BufferedReader reader;
		StringTokenizer tokenizer;

		public MyScanner(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}
		public void eat() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					line = reader.readLine();
					if (line == null) {
						tokenizer = null;
						return;
					}
					tokenizer = new StringTokenizer(line);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
		public String next() {
			eat();
			return tokenizer.nextToken();
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		public boolean hasNext() {
			eat();
			return (tokenizer != null && tokenizer.hasMoreElements());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++) a[i] = nextInt();
			return a;
		}
	}
}