#include <bits/stdc++.h>
using namespace std;
const int vc = 1e9 + 7;
const int maxN = 310;
const int maxM = maxN * maxN;
int r[maxN][maxN], le[maxN][maxN], ri[maxN][maxN], a[maxN][maxN];
int n, m, p;
int head[maxM], link[maxM], X[maxM], Y[maxM], W[maxM], Co[maxM];
void solve1(int i) {
  for (int j = 1; j <= n; j++) {
    le[j][0] = -1;
    ri[j][m + 1] = -1;
    for (int k = 1; k <= m; k++)
      if (a[j][k] == i - 1)
        le[j][k] = k;
      else
        le[j][k] = le[j][k - 1];
    for (int k = m; k > 0; k--)
      if (a[j][k] == i - 1)
        ri[j][k] = k;
      else
        ri[j][k] = ri[j][k + 1];
  }
  int t;
  t = head[i];
  while (t > 0) {
    int x, y;
    x = X[t];
    y = Y[t];
    for (int j = 1; j <= n; j++) {
      if (le[j][y] > -1 &&
          (r[j][le[j][y]] + abs(j - x) + abs(le[j][y] - y) < r[x][y]))
        r[x][y] = r[j][le[j][y]] + abs(j - x) + abs(le[j][y] - y);
      if (ri[j][y] > -1 &&
          (r[j][ri[j][y]] + abs(j - x) + abs(ri[j][y] - y) < r[x][y]))
        r[x][y] = r[j][ri[j][y]] + abs(j - x) + abs(ri[j][y] - y);
    }
    if (i == p) cout << r[x][y];
    t = link[t];
  }
}
void solve2(int i) {
  int t, t2, x, x2, y, y2;
  t = head[i];
  while (t > 0) {
    x = X[t];
    y = Y[t];
    t2 = head[i - 1];
    while (t2 > 0) {
      x2 = X[t2];
      y2 = Y[t2];
      if (r[x2][y2] + abs(x - x2) + abs(y - y2) < r[x][y])
        r[x][y] = r[x2][y2] + abs(x - x2) + abs(y - y2);
      t2 = link[t2];
    }
    if (i == p) cout << r[x][y];
    t = link[t];
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  if (p == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= p; i++) head[i] = 0;
  for (int i = 1; i <= p; i++) Co[i] = 0;
  long long P = 0, MaxC = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      Co[a[i][j]]++;
      P++;
      X[P] = i;
      Y[P] = j;
      W[P] = a[i][j];
      link[P] = head[W[P]];
      head[W[P]] = P;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 1)
        r[i][j] = i + j - 2;
      else
        r[i][j] = vc;
  for (int i = 2; i <= p; i++)
    if (Co[i] * Co[i - 1] >= n * m)
      solve1(i);
    else
      solve2(i);
  return 0;
}
