#include <bits/stdc++.h>
using namespace std;
int mat[303][303], n, m, p = 2;
int dp[303][303];
vector<pair<int, int> > all[303 * 303];
vector<int> v[303];
bool vis[303];
int main() {
  int i, j;
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &p);
  if (p == 1) return cout << 0 << endl, 0;
  memset(dp, 0x3f3f3f3f, sizeof(dp));
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &mat[i][j]);
      all[mat[i][j]].push_back({i, j});
      if (mat[i][j] == 1) dp[i][j] = i + j;
    }
  }
  for (int a = 1; a < p; a++) {
    for (i = 0; i < m; i++) v[i].clear();
    memset(vis, false, sizeof(vis));
    for (auto xx : all[a + 1]) {
      int x = xx.first, y = xx.second;
      v[y].push_back(x);
    }
    for (auto xx : all[a]) vis[xx.first] = true;
    for (i = 0; i < n; i++) {
      if (vis[i] == false) continue;
      int best = 0x3f3f3f3f;
      for (j = 0; j < m; j++) {
        if (mat[i][j] == a)
          best = dp[i][j];
        else
          best = min(0x3f3f3f3f, best + 1);
        if (best == 0x3f3f3f3f) continue;
        for (auto x : v[j]) {
          dp[x][j] = min(dp[x][j], best + abs(i - x));
        }
      }
      best = 0x3f3f3f3f;
      for (j = m - 1; j >= 0; j--) {
        if (mat[i][j] == a)
          best = dp[i][j];
        else
          best = min(0x3f3f3f3f, best + 1);
        if (best == 0x3f3f3f3f) continue;
        for (auto x : v[j]) {
          dp[x][j] = min(dp[x][j], best + abs(i - x));
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (mat[i][j] == p) return cout << dp[i][j] << endl, 0;
    }
  }
}
