#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 123;
const int inf = (int)2e9 + 123;
const int mod = 1e9 + 7;
using namespace std;
int n, m, p, a[311][311];
int dp[311][311];
vector<pair<int, int> > c[300 * 300 + 123];
vector<int> col[311];
bool is[311];
int main() {
  unsigned int FOR;
  asm("rdtsc" : "=A"(FOR));
  srand(FOR);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      dp[i][j] = inf;
      if (a[i][j] == 1) {
        dp[i][j] = (i - 1) + (j - 1);
      }
      c[a[i][j]].push_back(make_pair(i, j));
    }
  }
  for (int k = 1; k < p; k++) {
    memset(is, 0, sizeof(is));
    for (int i = 1; i <= m; i++) {
      col[i].clear();
    }
    for (auto to : c[k + 1]) {
      col[to.second].push_back(to.first);
    }
    for (auto to : c[k]) {
      is[to.first] = 1;
    }
    for (int i = 1; i <= n; i++) {
      if (!is[i]) continue;
      int mn = inf;
      for (int j = 1; j <= m; j++) {
        mn++;
        if (a[i][j] == k) {
          mn = min(mn, dp[i][j]);
        }
        for (auto to : col[j]) {
          dp[to][j] = min(dp[to][j], mn + abs(to - i));
        }
      }
      mn = inf;
      for (int j = m; j >= 1; j--) {
        mn++;
        if (a[i][j] == k) {
          mn = min(mn, dp[i][j]);
        }
        for (auto to : col[j]) {
          dp[to][j] = min(dp[to][j], mn + abs(to - i));
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == p) {
        cout << dp[i][j] << " ";
        return 0;
      }
    }
  }
  return 0;
}
