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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.BitSet;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        MyScanner in = new MyScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        private static final int[] I = {0, 1, 0, -1, 1, 0, -1, 0};
        private static BitSet done = new BitSet(154000);
        private static int[][] DP;
        private static int[][] palace;
        private static int[][] tempDP;
        private static Map<Integer, List<TaskD.Cell>> color;
        private static List<TaskD.Cell> sorted;
        private static int[] q;
        private static int N;
        private static int M;
        private static int P;
        private static int h;
        private static int t;
        private static int currIndex;
        private static final int CONST = 511;

        public void solve(int testNumber, MyScanner in, PrintWriter out) {
            N = in.nextInt();
            M = in.nextInt();
            P = in.nextInt();
            DP = new int[N][M];
            tempDP = new int[N][M];
            palace = new int[N][M];
            color = new HashMap<>();
            sorted = new ArrayList<>();
            int pr = 0, pc = 0;
            for (int i = 0; i < N; i++)
                Arrays.fill(DP[i], Integer.MAX_VALUE);

            for (int i = 0; i < N; i++)
                for (int j = 0; j < M; j++) {
                    palace[i][j] = in.nextInt();
                    List<TaskD.Cell> list = color.get(palace[i][j]);
                    if (list == null)
                        list = new ArrayList<>();
                    list.add(new TaskD.Cell(i, j));
                    color.put(palace[i][j], list);
                    if (palace[i][j] == 1)
                        DP[i][j] = i + j;
                    if (palace[i][j] == P) {
                        pr = i;
                        pc = j; //store the dest row, col
                    }
                }

            q = new int[N * M];
            for (int i = 1; i < P; i++) {
                List<TaskD.Cell> source = color.get(i);
                List<TaskD.Cell> dest = color.get(i + 1);
                int sourceCnt = source.size();
                int destCnt = dest.size();
                if (sourceCnt * destCnt <= N * M * 2) {
                    for (TaskD.Cell s : source)
                        for (TaskD.Cell d : dest)
                            DP[d.r][d.c] = Math.min(DP[d.r][d.c], DP[s.r][s.c] + Math.abs(d.r - s.r) + Math.abs(d.c - s.c));
                } else {
                    h = 0;
                    t = 0;
                    done.clear();
                    sorted.clear();
                    for (TaskD.Cell s : source) {
                        s.distance = DP[s.r][s.c];
                        sorted.add(s);
                    }
                    Collections.sort(sorted, (o1, o2) -> Integer.compare(o1.distance, o2.distance));
                    TaskD.Cell first = sorted.get(0);
                    q[t++] = (first.r << 9) + first.c;
                    tempDP[first.r][first.c] = first.distance;
                    done.set((first.r << 9) + first.c);
                    currIndex = 1;
                    bfs(i + 1, sourceCnt, destCnt);
                }
            }
            out.println(DP[pr][pc]);
        }

        private static void bfs(int dest, int sourceCnt, int destCnt) {
            int dCnt = 0;
            while (h < t && dCnt < destCnt) {
                int cell = q[h++];
                int r = cell >> 9;
                int c = cell & CONST;

                while ((currIndex < sourceCnt)
                        && (sorted.get(currIndex).distance == tempDP[r][c]))

                {
                    TaskD.Cell curr = sorted.get(currIndex++);
                    int rowCol = (curr.r << 9) + curr.c;
                    if (!done.get(rowCol)) {
                        q[t++] = rowCol;
                        done.set(rowCol);
                        tempDP[curr.r][curr.c] = curr.distance;
                    }
                }

                for (int i = 0; i < 8; i += 2) {
                    int newR = r + I[i];
                    int newC = c + I[i + 1];
                    if (newR >= 0 && newC >= 0 && newR < N && newC < M && !done.get((newR << 9) + newC)) {
                        int newRC = (newR << 9) + newC;
                        done.set(newRC);
                        if (palace[newR][newC] == dest) {
                            DP[newR][newC] = tempDP[r][c] + 1;
                            ++dCnt;
                        }
                        tempDP[newR][newC] = tempDP[r][c] + 1;
                        q[t++] = newRC;
                    }
                }
            }

        }

        static class Cell {
            int r;
            int c;
            int distance;

            Cell(int r, int c) {
                this.r = r;
                this.c = c;
            }

        }

    }

    static class MyScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public MyScanner(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return parseInt(next());
        }

        public int parseInt(String in) {
            // Check for a sign.
            int num = 0, sign = -1, i = 0;
            final int len = in.length();
            final char ch = in.charAt(0);
            if (ch == '-')
                sign = 1;
            else
                num = '0' - ch;

            // Build the number.
            i += 1;
            while (i < len)
                num = num * 10 + '0' - in.charAt(i++);
            return sign * num;
        }

    }
}

