#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, times;
};
int T, t, n, m, P, INF = 1e9;
int val[310][310];
long long num[90010];
vector<node> vc[90010];
int _x[4] = {0, 1, 0, -1}, _y[4] = {-1, 0, 1, 0};
int vis[310][310];
vector<node> vc2[610];
void bfs(int K) {
  int i, j, k, k2, x, y, x2, y2, minn = 1e9, ret, len;
  node A;
  memset(vis, -1, sizeof(vis));
  for (i = 0; i <= 605; i++) vc2[i].clear();
  for (i = 0; i < num[K]; i++) minn = min(minn, vc[K][i].times);
  for (i = 0; i < num[K]; i++) {
    ret = vc[K][i].times - minn;
    if (ret <= 600) vc2[ret].push_back(vc[K][i]);
  }
  for (k = 0; k <= 600; k++) {
    len = vc2[k].size();
    for (i = 0; i < len; i++) {
      x = vc2[k][i].x;
      y = vc2[k][i].y;
      vis[x][y] = minn + k;
      for (k2 = 0; k2 < 4; k2++) {
        x2 = x + _x[k2];
        y2 = y + _y[k2];
        if (1 <= x2 && x2 <= n && 1 <= y2 && y2 <= m && vis[x2][y2] == -1) {
          vis[x2][y2] = minn + k + 1;
          A.x = x2;
          A.y = y2;
          vc2[k + 1].push_back(A);
        }
      }
    }
  }
}
int main() {
  int i, j, k, ans;
  node A;
  scanf("%d%d%d", &n, &m, &P);
  A.times = 1e9;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      scanf("%d", &val[i][j]);
      A.x = i;
      A.y = j;
      vc[val[i][j]].push_back(A);
    }
  A.x = 1;
  A.y = 1;
  A.times = 0;
  vc[0].push_back(A);
  for (i = 0; i <= P; i++) num[i] = vc[i].size();
  for (k = 0; k < P; k++) {
    if (num[k] * num[k + 1] <= 90000) {
      for (i = 0; i < num[k]; i++)
        for (j = 0; j < num[k + 1]; j++) {
          vc[k + 1][j].times =
              min(vc[k + 1][j].times, vc[k][i].times +
                                          abs(vc[k][i].x - vc[k + 1][j].x) +
                                          abs(vc[k][i].y - vc[k + 1][j].y));
        }
    } else {
      bfs(k);
      for (j = 0; j < num[k + 1]; j++)
        vc[k + 1][j].times = vis[vc[k + 1][j].x][vc[k + 1][j].y];
    }
  }
  ans = 1e9;
  for (i = 0; i < num[P]; i++) ans = min(ans, vc[P][i].times);
  printf("%d\n", ans);
}
