#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int ex[300000], ey[300000], nxt[300000], first[300000];
int ux[300000], uy[300000], uxt[300000], uirst[3000];
int c[300000], d[600][600], e[600][600];
int i, j, k, l, m, n, o, r, s, t, x, y, sum_edge, sum_udge;
inline void addedge(int x, int y, int z) {
  sum_edge++, ex[sum_edge] = x, ey[sum_edge] = y, nxt[sum_edge] = first[z],
              first[z] = sum_edge;
  return;
}
inline void addudge(int x, int y, int z) {
  sum_udge++, ux[sum_udge] = x, uy[sum_udge] = y, uxt[sum_udge] = uirst[z],
              uirst[z] = sum_udge;
  return;
}
int main() {
  scanf("%d%d%d", &n, &m, &o);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) scanf("%d", &t), addedge(i, j, t), c[t]++;
  memset(d, 12, sizeof(d)), s = 100000000;
  for (i = first[1]; i != 0; i = nxt[i]) d[ex[i]][ey[i]] = ex[i] + ey[i] - 2;
  for (i = 1; i < o; i++) {
    if (c[i] * c[i + 1] < n * m)
      for (j = first[i]; j != 0; j = nxt[j])
        for (k = first[i + 1]; k != 0; k = nxt[k])
          d[ex[k]][ey[k]] =
              min(d[ex[k]][ey[k]],
                  d[ex[j]][ey[j]] + abs(ex[j] - ex[k]) + abs(ey[j] - ey[k]));
    else {
      r = 100000000;
      for (j = first[i]; j != 0; j = nxt[j]) r = min(r, d[ex[j]][ey[j]]);
      memset(e, 255, sizeof(e));
      memset(uirst, 0, sizeof(uirst));
      sum_udge = 0;
      for (j = first[i]; j != 0; j = nxt[j])
        addudge(ex[j], ey[j], d[ex[j]][ey[j]] - r),
            e[ex[j]][ey[j]] = d[ex[j]][ey[j]] - r;
      for (j = 0; j < n + m; j++)
        for (k = uirst[j]; k != 0; k = uxt[k])
          for (l = 0; l < 4; l++) {
            x = ux[k] + dx[l], y = uy[k] + dy[l];
            if ((x > 0) && (x <= n) && (y > 0) && (y <= m) && (!(~e[x][y])))
              addudge(x, y, j + 1), e[x][y] = j + 1;
          }
      for (j = first[i + 1]; j != 0; j = nxt[j])
        d[ex[j]][ey[j]] = e[ex[j]][ey[j]] + r;
    }
  }
  for (i = first[o]; i != 0; i = nxt[i]) s = min(s, d[ex[i]][ey[i]]);
  printf("%d", s);
  return 0;
}
