#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 17;
const int MX = (int)1e6 + 17;
const int MOD = (int)1e9 + 7;
const long long oo = LLONG_MAX;
const int INF = INT_MAX;
const long double Pi = 3.14159265358979323846264338327950288419716939937510;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};
inline long long IN() {
  long long x = 0, ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void OUT(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
void IOI2017() {}
int n, m, p, x;
vector<pair<int, pair<int, int> > > G[N];
int Get(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
int main() {
  n = IN(), m = IN(), p = IN();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      x = IN();
      G[x].push_back({MOD, {i, j}});
    }
  }
  G[0].push_back({0, {1, 1}});
  for (int i = 1; i <= p; i++) {
    sort(G[i - 1].begin(), G[i - 1].end());
    for (int j = 0; j < (int)G[i].size(); j++) {
      for (int k = 0; k < min((int)G[i - 1].size(), 1000); k++) {
        G[i][j].first =
            min(G[i][j].first,
                G[i - 1][k].first +
                    Get({G[i][j].second.first, G[i][j].second.second},
                        {G[i - 1][k].second.first, G[i - 1][k].second.second}));
      }
    }
  }
  cout << G[p][0].first;
  return 0;
}
