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 final class vanya_and_treasure
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();
	static int[] tree1,tree2;
	static ArrayList<Node>[] al;
	static int[][] dp,a;
	
	static void build(int node,int s,int e)
	{
		if(s>e)
		{
			return;
		}
		if(s==e)
		{
			tree1[node]=tree2[node]=Integer.MAX_VALUE;
		}
		else
		{
			int mid=(s+e)>>1;
			build(node<<1,s,mid);build(node<<1|1,mid+1,e);
			tree1[node]=Math.min(tree1[node<<1],tree1[node<<1|1]);
			tree2[node]=Math.min(tree2[node<<1],tree2[node<<1|1]);
		}
	}
	
	static void update1(int node,int s,int e,int l,int r,int val)
	{
		if(s>e || l>e || r<s)
		{
			return;
		}
		if(s==e)
		{
			tree1[node]=Math.min(tree1[node],val);
		}
		else
		{
			int mid=(s+e)>>1;
			update1(node<<1,s,mid,l,r,val);update1(node<<1|1,mid+1,e,l,r,val);
			tree1[node]=Math.min(tree1[node<<1],tree1[node<<1|1]);
		}
	}
	
	static int query1(int node,int s,int e,int l,int r)
	{
		if(s>e || l>e || r<s)
		{
			return Integer.MAX_VALUE;
		}
		if(l<=s && r>=e)
		{
			return tree1[node];
		}
		else
		{
			int mid=(s+e)>>1;
			int val1=query1(node<<1,s,mid,l,r),val2=query1(node<<1|1,mid+1,e,l,r);
			return Math.min(val1,val2);
		}
	}
	
	static void update2(int node,int s,int e,int l,int r,int val)
	{
		if(s>e || l>e || r<s)
		{
			return;
		}
		if(s==e)
		{
			tree2[node]=Math.min(tree2[node],val);
		}
		else
		{
			int mid=(s+e)>>1;
			update2(node<<1,s,mid,l,r,val);update2(node<<1|1,mid+1,e,l,r,val);
			tree2[node]=Math.min(tree2[node<<1],tree2[node<<1|1]);
		}
	}
	
	static int query2(int node,int s,int e,int l,int r)
	{
		if(s>e || l>e || r<s)
		{
			return Integer.MAX_VALUE;
		}
		if(l<=s && r>=e)
		{
			return tree2[node];
		}
		else
		{
			int mid=(s+e)>>1;
			int val1=query2(node<<1,s,mid,l,r),val2=query2(node<<1|1,mid+1,e,l,r);
			return Math.min(val1,val2);
		}
	}
	
	@SuppressWarnings("unchecked")
    public static void main(String args[]) throws Exception
    {
		int n=sc.nextInt(),m=sc.nextInt(),p=sc.nextInt();al=new ArrayList[p+1];dp=new int[n+1][m+1];a=new int[n+1][m+1];
		for(int i=1;i<=p;i++)
		{
			al[i]=new ArrayList<Node>();
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				a[i][j]=sc.nextInt();al[a[i][j]].add(new Node(i,j));
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				dp[i][j]=Integer.MAX_VALUE;
			}
		}
		for(Node x:al[1])
		{
			dp[x.i][x.j]=x.i-1+x.j-1;
		}
		tree1=new int[4*(m+1)];tree2=new int[4*(m+1)];
		for(int i=2;i<=p;i++)
		{
			List<Node> list=new ArrayList<Node>();
			for(Node x:al[i-1])
			{
				list.add(x);
			}
			for(Node x:al[i])
			{
				list.add(x);
			}
			Collections.sort(list);build(1,1,m);
			for(int j=0;j<list.size();j++)
			{
				Node curr=list.get(j);
				if(a[curr.i][curr.j]==i-1)
				{
					update1(1,1,m,curr.j,curr.j,dp[curr.i][curr.j]-curr.i-curr.j);
					update2(1,1,m,curr.j,curr.j,dp[curr.i][curr.j]-curr.i+curr.j);
				}
				else
				{
					int val1=query1(1,1,m,1,curr.j);
					if(val1<Integer.MAX_VALUE)
					{
						dp[curr.i][curr.j]=Math.min(dp[curr.i][curr.j],val1+curr.i+curr.j);
					}
					int val2=query2(1,1,m,curr.j,m);
					if(val2<Integer.MAX_VALUE)
					{
						dp[curr.i][curr.j]=Math.min(dp[curr.i][curr.j],val2+curr.i-curr.j);
					}
				}
			}
			Collections.reverse(list);build(1,1,m);
			for(int j=0;j<list.size();j++)
			{
				Node curr=list.get(j);
				if(a[curr.i][curr.j]==i-1)
				{
					update1(1,1,m,curr.j,curr.j,dp[curr.i][curr.j]+curr.i-curr.j);
					update2(1,1,m,curr.j,curr.j,dp[curr.i][curr.j]+curr.i+curr.j);
				}
				else
				{
					int val1=query1(1,1,m,1,curr.j);
					if(val1<Integer.MAX_VALUE)
					{
						dp[curr.i][curr.j]=Math.min(dp[curr.i][curr.j],val1-curr.i+curr.j);
					}
					int val2=query2(1,1,m,curr.j,m);
					if(val2<Integer.MAX_VALUE)
					{
						dp[curr.i][curr.j]=Math.min(dp[curr.i][curr.j],val2-curr.i-curr.j);
					}
				}
			}
		}
		int res=Integer.MAX_VALUE;
		for(Node x:al[p])
		{
			res=Math.min(res,dp[x.i][x.j]);
		}
		out.println(res);out.close();
	}
}
class Node implements Comparable<Node>
{
	int i,j;
	public Node(int i,int j)
	{
		this.i=i;this.j=j;
	}
	public int compareTo(Node x)
	{
		return Integer.compare(this.i,x.i);
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}