#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int a[N][N], dp[N][N];
vector<pair<int, int> > g[N * N];
const int inf = 1e9;
int vis[N];
int main() {
  int n, m, p;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      g[a[i][j]].push_back(make_pair(i, j));
      if (a[i][j] == 1) {
        dp[i][j] = i + j - 2;
      } else {
        dp[i][j] = inf;
      }
    }
  }
  for (int i = 1; i < p; i++) {
    vector<int> cur[m + 5];
    for (auto it : g[i + 1]) {
      cur[it.second].push_back(it.first);
    }
    for (auto it : g[i]) {
      vis[it.first] = i;
    }
    for (int j = 1; j <= n; j++) {
      if (vis[j] == i) {
        int best = inf;
        for (int k = 1; k <= m; k++) {
          best++;
          if (a[j][k] == i) {
            best = min(best, dp[j][k]);
          }
          for (auto it : cur[k]) {
            dp[it][k] = min(dp[it][k], best + abs(it - j));
          }
        }
        best = inf;
        for (int k = m; k >= 1; k--) {
          best++;
          if (a[j][k] == i) {
            best = min(best, dp[j][k]);
          }
          for (auto it : cur[k]) {
            dp[it][k] = min(dp[it][k], best + abs(it - j));
          }
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == p) {
        printf("%d\n", dp[i][j]);
      }
    }
  }
}
