#include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
int n, m, p;
int arr[MAXN][MAXN];
vector<pair<int, int> > pos[MAXN * MAXN];
int ma[MAXN * MAXN];
int cnt = 0;
int dist[MAXN][MAXN][MAXN];
int xx[4] = {-1, 1, 0, 0};
int yy[4] = {0, 0, -1, 1};
int dp[MAXN][MAXN];
int di[MAXN][MAXN];
int ur[MAXN][MAXN];
int ul[MAXN][MAXN];
int dr[MAXN][MAXN];
int dl[MAXN][MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> p;
  for (int g = 1; g <= n; g++)
    for (int y = 1; y <= m; y++) cin >> arr[g][y];
  for (int g = 1; g <= n; g++)
    for (int y = 1; y <= m; y++) pos[arr[g][y]].push_back(pair<int, int>(g, y));
  for (int g = 1; g <= n; g++)
    for (int y = 1; y <= m; y++) dp[g][y] = 1e9;
  for (int g = 1; g <= n; g++)
    for (int y = 1; y <= m; y++) {
      if (arr[g][y] == p) dp[g][y] = 0;
    }
  for (int g = p - 1; g >= 1; g--) {
    if (pos[g + 1].size() >= 300) {
      for (int g = 1; g <= n; g++)
        for (int y = 1; y <= m; y++) {
          di[g][y] = ur[g][y] = ul[g][y] = dr[g][y] = dl[g][y] = 1e9;
        }
      for (pair<int, int> t : pos[g + 1]) {
        di[t.first][t.second] = dp[t.first][t.second];
      }
      for (int y = 1; y <= n; y++) {
        for (int z = 1; z <= m; z++) {
          ul[y][z] = di[y][z];
          if (y != 1) ul[y][z] = min(ul[y][z], 1 + ul[y - 1][z]);
          if (z != 1) ul[y][z] = min(ul[y][z], 1 + ul[y][z - 1]);
        }
      }
      for (int y = n; y >= 1; y--) {
        for (int z = m; z >= 1; z--) {
          dr[y][z] = di[y][z];
          if (y != n) dr[y][z] = min(dr[y][z], 1 + dr[y + 1][z]);
          if (z != m) dr[y][z] = min(dr[y][z], 1 + dr[y][z + 1]);
        }
      }
      for (int y = n; y >= 1; y--) {
        for (int z = 1; z <= m; z++) {
          dl[y][z] = di[y][z];
          if (y != n) dl[y][z] = min(dl[y][z], 1 + dl[y + 1][z]);
          if (z != 1) dl[y][z] = min(dl[y][z], 1 + dl[y][z - 1]);
        }
      }
      for (int y = 1; y <= n; y++) {
        for (int z = m; z >= 1; z--) {
          ur[y][z] = di[y][z];
          if (y != 1) ur[y][z] = min(ur[y][z], 1 + ur[y - 1][z]);
          if (z != m) ur[y][z] = min(ur[y][z], 1 + ur[y][z + 1]);
        }
      }
      for (pair<int, int> t : pos[g]) {
        int a = t.first, b = t.second;
        dp[a][b] = min({ul[a][b], ur[a][b], dl[a][b], dr[a][b]});
      }
    } else {
      for (pair<int, int> t : pos[g]) {
        for (pair<int, int> y : pos[g + 1]) {
          dp[t.first][t.second] =
              min(dp[t.first][t.second], abs(t.first - y.first) +
                                             abs(t.second - y.second) +
                                             dp[y.first][y.second]);
        }
      }
    }
  }
  int answer = 2e9;
  for (int g = 1; g <= n; g++) {
    for (int y = 1; y <= m; y++) {
      if (arr[g][y] == 1) {
        answer = min(answer, g + y - 2 + dp[g][y]);
      }
    }
  }
  cout << answer << '\n';
  return 0;
}
