#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 3e2 + 5;
queue<pair<int, int> > q;
vector<pair<int, int> > v[N * N];
int n, m, p;
int a[N][N], f[N][N];
int *ans;
int dis(int x, int y, int a, int b) { return abs(x - a) + abs(y - b); }
void init() {
  for (int i = 0; i <= n * m; i++) v[i].clear();
  memset(f, inf, sizeof(f));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]), v[a[i][j]].push_back(pair<int, int>(i, j));
      if (a[i][j] == 1) f[i][j] = abs(1 - i) + abs(1 - j);
      if (a[i][j] == p) ans = &f[i][j];
    }
  }
}
int dx[] = {-1, 1, 0, 0}, w[N][N];
int dy[] = {0, 0, -1, 1};
void bfs(int dep) {
  memset(w, inf, sizeof(w));
  for (int i = 0; i < v[dep].size(); i++) {
    int x = v[dep][i].first, y = v[dep][i].second;
    q.push(v[dep][i]);
    w[x][y] = f[x][y];
  }
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    int x = t.first, y = t.second, dis = w[x][y];
    for (int i = 0; i < 4; i++) {
      int c = x + dx[i], d = y + dy[i];
      if (c >= 1 && c <= n && d >= 1 && d <= m) {
        if (w[c][d] > dis + 1) {
          w[c][d] = dis + 1;
          if (a[c][d] == dep + 1) f[c][d] = dis + 1;
          q.push(pair<int, int>(c, d));
        }
      }
    }
  }
}
void solve() {
  int i, j, x, c, y, d;
  for (int k = 2; k <= p; k++) {
    if (v[k - 1].size() * v[k].size() < n * m) {
      for (i = 0; i < v[k - 1].size(); i++) {
        for (j = 0; j < v[k].size(); j++) {
          x = v[k][j].first, y = v[k][j].second;
          c = v[k - 1][i].first, d = v[k - 1][i].second;
          f[x][y] = min(f[x][y], f[c][d] + dis(x, y, c, d));
        }
      }
    } else
      bfs(k - 1);
  }
  printf("%d\n", *ans);
}
int main() {
  cin >> n >> m >> p;
  init();
  solve();
  return 0;
}
