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.*;

public class Main {

    final static int INF = 1 << 30;

    static class Pair implements Comparable<Pair> {
        int i, j;
        int dist;
        public int compareTo(Pair o) {
            if (dist != o.dist)
                return dist - o.dist;
            return i == o.i ? j - o.j : i - o.i;
        }
        Pair(int _i, int _j) {
            i = _i; j = _j;
        }
        Pair(int _i, int _j, int _dist) {
            i = _i; j = _j; dist = _dist;
        }
        int distTo(Pair o) {
            return Math.abs(i - o.i) + Math.abs(j - o.j);
        }
    }

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

    void compute(int n, int m, ArrayList<Pair> first, ArrayList<Pair> second) {
        // moving from first to second;
        //out.println("Going bfs")

        if (1L * first.size() * second.size() > 1L * n * m * Math.max(n, m) / 10) {
            int best[][] = new int[n][m];
            for (int i = 0 ; i < n ; i ++) {
                Arrays.fill(best[i], INF);
            }

            PriorityQueue<Pair> queue = new PriorityQueue<Pair>();
            for (Pair f : first) {
                best[f.i][f.j] = f.dist;
                queue.add(f);
            }

            while (!queue.isEmpty()) {
                Pair current = queue.poll();
                if (best[current.i][current.j] != current.dist) 
                    continue;

                for (int k = 0 ; k < 4 ; k ++) {
                    int ni = current.i + dx[k], nj = current.j + dy[k];
                    if (0 <= ni && ni < n && 0 <= nj && nj < m && best[ni][nj] > current.dist + 1) {
                        best[ni][nj] = current.dist + 1;
                        queue.add(new Pair(ni, nj, current.dist+1));
                    }
                }
                
            }

            for (Pair s : second) {
                s.dist = best[s.i][s.j];
            }



            
        } else {
            for (Pair s : second) {
                Pair best = first.get(0);

                for (int i = 1 ; i < first.size() ; i ++) {
                    Pair target = first.get(i);
                    if (s.distTo(best) + best.dist > s.distTo(target) + target.dist)
                        best = target;
                }
                s.dist = s.distTo(best) + best.dist;
            }

        }
    }
    ArrayList< ArrayList<Pair> > idx;


    int a[][];
    int dp[][];



    public void solve() throws IOException {
        int n = nextInt(), m = nextInt(), p = nextInt();
        a = new int[n][m];
        dp = new int[n][m];

        idx = new ArrayList< ArrayList<Pair> >();
        for (int i = 0 ; i < p ; i ++) 
            idx.add(new ArrayList<Pair>());


        for (int i = 0 ; i < n ; i ++) {
            for (int j = 0 ; j < m ; j ++) {
                a[i][j] = nextInt()-1;
                idx.get(a[i][j]).add(new Pair(i, j));
            }
        }   

        ArrayList<Pair> first = idx.get(0);
        for (Pair f: first) {
            f.dist = f.i + f.j;
        }
        for (int pp = 1 ; pp < p ; pp++) {
            compute(n,m, idx.get(pp-1), idx.get(pp));
        }

        out.println(idx.get(p-1).get(0).dist);
    }
    

    
    
    BufferedReader bf;
    StringTokenizer st;
    PrintWriter out;
    
    public String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) 
            st = new StringTokenizer(bf.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public Main() throws IOException {
        bf = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));		
        solve();
        bf.close();
        out.close();
    }
    
    public static void main(String args[]) throws IOException {
        new Main();
    }
}