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.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Queue;

public class VanyaAndTreasure {
    public static void main(String[] args) {
        FasterScanner sc = new FasterScanner();
        
        int N = sc.nextInt();
        int M = sc.nextInt();
        int P = sc.nextInt();
        
        ChestList[] level = new ChestList[P + 1];
        for (int i = 0; i <= P; i++) {
        	level[i] = new ChestList();
        }
        
        Chest[][] map = new Chest[N][M];
        for (int i = 0; i < N; i++) {
        	for (int j = 0; j < M; j++) {
        		int A = sc.nextInt();
        		map[i][j] = new Chest(i, j, A);
        		level[A].add(map[i][j]);
        	}
        }
        
        Chest init = new Chest(0, 0, 0);
        init.dist = 0;
        level[0].add(init);
        
        for (int i = 0; i < P; i++) {
        	if (level[i].size() * level[i + 1].size() >= 8 * N * M) {
        		floodFill(level[i], level[i + 1], N, M);
        	} else {
        		sparseLevel(level[i], level[i + 1]);
        	}
        }
        
        System.out.println(level[P].get(0).dist);
    }
    
    private static final int[] DR = { -1, 0, 1, 0 };
    private static final int[] DC = { 0, -1, 0, 1 };
    private static Comparator<Chest> BY_DIST = new Comparator<Chest>() {
    	public int compare(Chest c1, Chest c2) {
    		return Long.compare(c1.dist, c2.dist);
    	}
    };
    public static void floodFill(ChestList curr, ChestList next, int N, int M) {
    	long[][] all = new long[N][M];
    	for (int r = 0; r < all.length; r++) {
    		Arrays.fill(all[r], Long.MAX_VALUE);
    	}
    	
    	Collections.sort(curr, BY_DIST);
    	
    	int idx = 1;
    	Chest init = curr.get(0);
    	Queue<Integer> qr = new ArrayDeque<Integer>();
    	Queue<Integer> qc = new ArrayDeque<Integer>();
    	Queue<Long> qd = new ArrayDeque<Long>();
    	qr.add(init.R);
    	qc.add(init.C);
    	qd.add(init.dist);
    	all[init.R][init.C] = init.dist;
    	while (!qr.isEmpty()) {
    		int cr = qr.poll();
    		int cc = qc.poll();
    		long cd = qd.poll();
    		while (idx < curr.size() && curr.get(idx).dist == cd) {
    			Chest item = curr.get(idx++);
    			if (item.dist < all[item.R][item.C]) {
    				qr.add(item.R);
    				qc.add(item.C);
    				qd.add(item.dist);
    			}
    		}
    		long d = cd + 1;
    		for (int i = 0; i < DR.length; i++) {
    			int r = cr + DR[i];
    			int c = cc + DC[i];
    			if (r >= 0 && r < N && c >= 0 && c < M && d < all[r][c]) {
    				all[r][c] = d;
    				qr.add(r);
    				qc.add(c);
    				qd.add(d);
    			}
    		}
    	}
    	for (Chest v : next) {
    		v.dist = all[v.R][v.C];
    	}
    }
    
    public static void sparseLevel(ChestList curr, ChestList next) {
    	for (Chest u : curr) {
    		for (Chest v : next) {
    			v.dist = Math.min(v.dist, u.dist + calcDist(u, v));
    		}
    	}
    }
    
    public static int calcDist(Chest u, Chest v) {
    	return Math.abs(u.R - v.R) + Math.abs(u.C - v.C);
    }
    
    public static class ChestList extends ArrayList<Chest> {
		private static final long serialVersionUID = 68152975692867707L;
    }
    
    public static class Chest {
    	public int R, C;
    	public int key;
    	
    	public long dist;
    	
    	public Chest(int r, int c, int k) {
    		this.R = r;
    		this.C = c;
    		this.key = k;
    		this.dist = Long.MAX_VALUE;
    	}
    }

	public static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = System.in.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public int[] nextIntArray(int n, int offset) {
			int[] arr = new int[n + offset];
			for (int i = 0; i < n; i++) {
				arr[i + offset] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public long[] nextLongArray(int n, int offset) {
			long[] arr = new long[n + offset];
			for (int i = 0; i < n; i++) {
				arr[i + offset] = nextLong();
			}
			return arr;
		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
	}
}