#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 100010;
const int MAX = 2000000000;
const int mod = 100000000;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;
const double EPS = 0.00000001;
const long long MOD = 1000000007;
const int INF = 1000000010;
const double pi = acos(-1.0);
struct node {
  int x, y, z;
  node() {}
  node(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
};
int f[310][310];
vector<node> q[90010];
int main() {
  int a, i, j, k, n, m, p;
  scanf("%d%d%d", &n, &m, &p);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      scanf("%d", &a);
      q[a].push_back(node(i, j, MAX));
    }
  for (i = 0; i < q[1].size(); i++)
    q[1][i].z = abs(q[1][i].x - 1) + abs(q[1][i].y - 1);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) f[i][j] = MAX;
  for (i = 1; i < p; i++) {
    for (j = 0; j < q[i].size(); j++)
      for (k = 1; k <= m; k++)
        f[q[i][j].x][k] = min(f[q[i][j].x][k], q[i][j].z + abs(k - q[i][j].y));
    for (j = 0; j < q[i + 1].size(); j++)
      for (k = 1; k <= n; k++)
        q[i + 1][j].z =
            min(q[i + 1][j].z, f[k][q[i + 1][j].y] + abs(k - q[i + 1][j].x));
    for (j = 0; j < q[i].size(); j++)
      for (k = 1; k <= m; k++) f[q[i][j].x][k] = MAX;
  }
  printf("%d\n", q[p][0].z);
  return 0;
}
