#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXH = 300;
const int MAXW = 300;
const int MAXVAL = 300 * 300;
const int MAXD = MAXH + MAXW - 2;
const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1};
int h, w, nval, dmx;
int t[MAXH][MAXW];
int dst[MAXH][MAXW];
int cnt[MAXVAL];
int xhead[MAXVAL], yhead[MAXVAL], xnxt[MAXH][MAXW], ynxt[MAXH][MAXW];
int qxhead[MAXD + 1], qyhead[MAXD + 1], qxnxt[MAXH][MAXW], qynxt[MAXH][MAXW];
int dp[MAXH][MAXW];
void calc(int z) {
  if (cnt[z] * cnt[z - 1] <= 5 * h * w) {
    for (int cx = xhead[z], cy = yhead[z], tc; cx != -1 && cy != -1;
         tc = xnxt[cx][cy], cy = ynxt[cx][cy], cx = tc) {
      dst[cx][cy] = INT_MAX;
      for (int px = xhead[z - 1], py = yhead[z - 1], tp; px != -1 && py != -1;
           tp = xnxt[px][py], py = ynxt[px][py], px = tp) {
        int cur = dst[px][py] + abs(cx - px) + abs(cy - py);
        if (cur < dst[cx][cy]) dst[cx][cy] = cur;
      }
    }
  } else {
    int mn = INT_MAX;
    for (int x = (0); x < (h); ++x)
      for (int y = (0); y < (w); ++y)
        if (t[x][y] == z - 1 && dst[x][y] < mn) mn = dst[x][y];
    for (int d = (0); d <= (dmx); ++d) qxhead[d] = qyhead[d] = -1;
    for (int x = (0); x < (h); ++x)
      for (int y = (0); y < (w); ++y) dp[x][y] = INT_MAX;
    for (int x = (0); x < (h); ++x)
      for (int y = (0); y < (w); ++y)
        if (t[x][y] == z - 1) {
          int d = dst[x][y] - mn;
          assert(d <= dmx);
          qxnxt[x][y] = qxhead[d], qynxt[x][y] = qyhead[d], qxhead[d] = x,
          qyhead[d] = y;
          dp[x][y] = dst[x][y];
        }
    for (int d = (0); d <= (dmx); ++d)
      for (int x = qxhead[d], y = qyhead[d], tmp; x != -1 && y != -1;
           tmp = qxnxt[x][y], y = qynxt[x][y], x = tmp)
        for (int k = (0); k < (4); ++k) {
          int nx = x + DX[k], ny = y + DY[k];
          if (nx < 0 || nx >= h || ny < 0 || ny >= w ||
              dp[nx][ny] <= dp[x][y] + 1)
            continue;
          assert(d + 1 <= dmx);
          qxnxt[nx][ny] = qxhead[d + 1], qynxt[nx][ny] = qyhead[d + 1],
          qxhead[d + 1] = nx, qyhead[d + 1] = ny;
          dp[nx][ny] = dp[x][y] + 1;
        }
    for (int x = (0); x < (h); ++x)
      for (int y = (0); y < (w); ++y)
        if (t[x][y] == z) dst[x][y] = dp[x][y];
  }
}
void run() {
  scanf("%d%d%d", &h, &w, &nval);
  dmx = h + w - 2;
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) scanf("%d", &t[x][y]), --t[x][y];
  for (int i = (0); i < (nval); ++i) cnt[i] = 0, xhead[i] = -1, yhead[i] = -1;
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y)
      xnxt[x][y] = xhead[t[x][y]], ynxt[x][y] = yhead[t[x][y]],
      xhead[t[x][y]] = x, yhead[t[x][y]] = y, ++cnt[t[x][y]];
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) dst[x][y] = t[x][y] == 0 ? x + y : -2;
  for (int i = (1); i < (nval); ++i) calc(i);
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y)
      if (t[x][y] == nval - 1) {
        printf("%d\n", dst[x][y]);
        return;
      }
  printf("-1\n");
}
int main() {
  run();
  return 0;
}
