#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
int a[303][303];
vector<pair<int, int> > A[300 * 300 + 12];
int Ds[303][303];
int Dst(pair<int, int> x, pair<int, int> y) {
  int ans = abs(x.first - y.first);
  ans += abs(x.second - y.second);
  return ans;
}
int main() {
  int n, m, k;
  n = in<int>(), m = in<int>(), k = in<int>();
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < m + 1; j++) {
      a[i][j] = in<int>();
      A[a[i][j]].push_back(make_pair(i, j));
    }
  if (k == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i < k + 1; i++) {
    random_shuffle(A[i].begin(), A[i].end());
  }
  int now = 0;
  for (int i = 0; i <= 300; i++) {
    for (int j = 0; j <= 300; j++) {
      Ds[i][j] = 200000000;
    }
  }
  for (int i = 0; i < A[1].size(); i++) {
    int nw = Dst(make_pair(1, 1), A[now + 1][i]);
    int x = 1;
    int y = 1;
    int xx = A[now + 1][i].first;
    int yy = A[now + 1][i].second;
    Ds[xx][yy] = min(Ds[xx][yy], nw);
  }
  now++;
  while (now < k) {
    for (int i = 0; i < A[now].size() && i < 5000; i++) {
      for (int j = 0; j < A[now + 1].size() && j < 5000; j++) {
        int nw = Dst(A[now][i], A[now + 1][j]);
        int x = A[now][i].first;
        int y = A[now][i].second;
        int xx = A[now + 1][j].first;
        int yy = A[now + 1][j].second;
        Ds[xx][yy] = min(Ds[xx][yy], Ds[x][y] + nw);
      }
    }
    now++;
  }
  printf("%d\n", Ds[A[k][0].first][A[k][0].second]);
  return 0;
}
