#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 500;
const int P = N * N;
const int DIFF = N + N;
vector<int> x[P], y[P];
int n, m, p, a[N][N], dp[N][N], bfs[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int abs(int x) { return x >= 0 ? x : -x; }
void bfsSolve(int lvl) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) bfs[i][j] = -1;
  int minDist = INF;
  for (int i = 0; i < x[lvl - 1].size(); i++)
    minDist = min(minDist, dp[x[lvl - 1][i]][y[lvl - 1][i]]);
  vector<int> vx[DIFF], vy[DIFF];
  for (int i = 0; i < x[lvl - 1].size(); i++) {
    int val = dp[x[lvl - 1][i]][y[lvl - 1][i]];
    vx[val - minDist].push_back(x[lvl - 1][i]);
    vy[val - minDist].push_back(y[lvl - 1][i]);
    bfs[x[lvl - 1][i]][y[lvl - 1][i]] = val - minDist;
  }
  for (int i = 0; i < DIFF; i++)
    for (int j = 0; j < vx[i].size(); j++)
      for (int k = 0; k < 4; k++) {
        int tx = vx[i][j] + dx[k];
        int ty = vy[i][j] + dy[k];
        if (tx >= 0 && tx < n && ty >= 0 && ty < m && bfs[tx][ty] == -1) {
          bfs[tx][ty] = i + 1;
          vx[i + 1].push_back(tx);
          vy[i + 1].push_back(ty);
        }
      }
  for (int i = 0; i < x[lvl].size(); i++)
    dp[x[lvl][i]][y[lvl][i]] = minDist + bfs[x[lvl][i]][y[lvl][i]];
}
void simpleSolve(int lvl) {
  for (int i = 0; i < x[lvl].size(); i++) {
    int tx = x[lvl][i];
    int ty = y[lvl][i];
    dp[tx][ty] = INF;
    for (int j = 0; j < x[lvl - 1].size(); j++) {
      int fx = x[lvl - 1][j];
      int fy = y[lvl - 1][j];
      dp[tx][ty] = min(dp[tx][ty], dp[fx][fy] + abs(tx - fx) + abs(ty - fy));
    }
  }
}
int main() {
  cin >> n >> m >> p;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      x[a[i][j] - 1].push_back(i);
      y[a[i][j] - 1].push_back(j);
    }
  for (int i = 0; i < x[0].size(); i++)
    dp[x[0][i]][y[0][i]] = x[0][i] + y[0][i];
  for (int i = 1; i < p; i++)
    if (x[i].size() * x[i - 1].size() >= n * m)
      bfsSolve(i);
    else
      simpleSolve(i);
  cout << dp[x[p - 1][0]][y[p - 1][0]] << endl;
  return 0;
}
