#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long INF = 1e18;
const int MAXN = 2e5 + 5;
const int MAXM = 9e4 + 500;
const int N = 300 + 10;
const double EPS = 1e-5;
int i, j, d;
int n, k, m, p;
int a[N][N], dp[2][N][N], last[N][N];
vector<pair<int, int> > grid[MAXM];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> p;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
      grid[a[i][j]].push_back({i, j});
    }
  }
  int ans = 1e9;
  for (int now = 1; now <= p; now++) {
    for (auto g : grid[now]) {
      int first = g.first;
      int second = g.second;
      int dist = 1e9;
      for (i = 1; i <= n; i++) {
        if (last[i][second] == now - 1)
          dist = min(dist, dp[1][i][second] + abs(first - i));
      }
      dp[0][first][second] =
          (now == 1 ? abs(first - 1) + abs(second - 1) : dist);
      if (now == p) ans = min(ans, dist);
    }
    for (auto g : grid[now]) {
      int first = g.first;
      int second = g.second;
      for (i = 1; i <= m; i++) {
        if (last[first][i] < now ||
            last[first][i] == now &&
                dp[0][first][second] + abs(second - i) < dp[1][first][i]) {
          last[first][i] = now;
          dp[1][first][i] = dp[0][first][second] + abs(second - i);
        }
      }
    }
  }
  cout << ans;
  return 0;
}
