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.*;
import java.util.*;

import static java.lang.Math.*;

public class Main extends PrintWriter {
    final Random rand = new Random(31);
    final int inf = (int) 1e9;
    final long linf = (long) 1e18;

    final static String IO = "_std";

    class Item implements Comparable<Item> {
        int d, x, y;

        public Item(int d, int x, int y) {
            this.d = d;
            this.x = x;
            this.y = y;
        }


        @Override
        public int compareTo(Item o) {
            return Integer.compare(d, o.d);
        }

        public Point getPoint() {
            return new Point(x, y);
        }
    }

    class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        int p = nextInt();
        List<Point>[] xs = createAdjacencyList(p + 1);
        boolean f1 = false;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int x = nextInt();
                if (i == 0 && j == 0 && x == 1) {
                    f1 = true;
                }
                xs[x].add(new Point(i, j));
            }
        }

        int[][] td = new int[n][m];
        for (int[] i : td) {
            Arrays.fill(i, inf);
        }
        td[0][0] = 0;
        xs[0].add(new Point(0, 0));

        int[] dx = new int[] {1, -1, 0, 0};
        int[] dy = new int[] {0, 0, 1, -1};


        int[][] d = new int[n][m];
        for (int t = 1; t <= p; t++) {
            if (t == 2 && !f1) {
                td[0][0] = inf;
            }
            if (xs[t].size() * xs[t - 1].size() <= n * m * 2) {
                for (Point vp : xs[t]) {
                    for (Point pt : xs[t - 1]) {
                        td[vp.x][vp.y] = min(td[vp.x][vp.y], td[pt.x][pt.y] + abs(pt.x - vp.x) + abs(pt.y - vp.y));
                    }
                }
            } else {
                Item[] willBeAddInFuture = new Item[xs[t - 1].size()];
                Queue<Point> q = new ArrayDeque<>();
                for (int[] i : d) {
                    Arrays.fill(i, inf);
                }

                int ind = 0;
                for (Point pt :  xs[t - 1]) {
                    willBeAddInFuture[ind++] = new Item(td[pt.x][pt.y], pt.x, pt.y);
                    d[pt.x][pt.y] = td[pt.x][pt.y];
                }

                Arrays.sort(willBeAddInFuture);

                int index = 0;
                while (true) {
                    if (index == willBeAddInFuture.length) break;
                    q.add(willBeAddInFuture[index++].getPoint());
                    while (!q.isEmpty()) {
                        Point v = q.poll();
                        int di = d[v.x][v.y];
                        while (index < willBeAddInFuture.length && willBeAddInFuture[index].d == di + 1) {
                            q.add(willBeAddInFuture[index++].getPoint());
                        }
                        for (int i = 0; i < 4; i++) {
                            int x = v.x + dx[i];
                            int y = v.y + dy[i];
                            if (x < 0 || x >= n || y < 0 || y >= m) {
                                continue;
                            }
                            if (d[x][y] > di + 1) {
                                d[x][y] = di + 1;
                                q.add(new Point(x, y));
                            }
                        }
                    }
                }

                for (Point pt : xs[t]) {
                    td[pt.x][pt.y] = d[pt.x][pt.y];
                }
            }
        }

        println(td[xs[p].get(0).x][xs[p].get(0).y]);
    }

    BufferedReader in;
    StringTokenizer stok;

    Main() {
        super(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
    }

    Main(String fileIn, String fileOut) throws IOException {
        super(fileOut);
        in = new BufferedReader(new FileReader(fileIn));
    }

    public static void main(String[] args) throws IOException {
        Main main;
        if ("_std".equals(IO)) {
            main = new Main();
        } else if ("_iotxt".equals(IO)) {
            main = new Main("input.txt", "output.txt");
        } else {
            main = new Main(IO + ".in", IO + ".out");
        }
        main.solve();
        main.close();
    }

    String next() throws IOException {
        while (stok == null || !stok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return null;
            }
            stok = new StringTokenizer(s);
        }
        return stok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    int[] nextIntArray(int len) throws IOException {
        int[] a = new int[len];
        for (int i = 0; i < len; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    int[] nextIntArraySorted(int len) throws IOException {
        int[] a = nextIntArray(len);
        shuffle(a);
        Arrays.sort(a);
        return a;
    }

    void shuffle(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int x = rand.nextInt(i + 1);
            int _ = a[i];
            a[i] = a[x];
            a[x] = _;
        }
    }

    void shuffleAndSort(int[] a) {
        shuffle(a);
        Arrays.sort(a);
    }

    boolean nextPermutation(int[] p) {
        for (int a = p.length - 2; a >= 0; --a) {
            if (p[a] < p[a + 1])
                for (int b = p.length - 1; ; --b)
                    if (p[b] > p[a]) {
                        int t = p[a];
                        p[a] = p[b];
                        p[b] = t;
                        for (++a, b = p.length - 1; a < b; ++a, --b) {
                            t = p[a];
                            p[a] = p[b];
                            p[b] = t;
                        }
                        return true;
                    }
        }
        return false;
    }

    <T> List<T>[] createAdjacencyList(int n) {
        List<T>[] res = new List[n];
        for (int i = 0; i < n; i++) {
            res[i] = new ArrayList<>();
        }
        return res;
    }

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