#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, m;
  cin >> n >> m;
  int p;
  cin >> p;
  int a[n][m];
  vector<pair<int, int>> v[p];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      a[i][j]--;
      v[a[i][j]].push_back({i, j});
    }
  }
  int dis[n][m];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      dis[i][j] = INF;
    }
  }
  int vis[n][m];
  memset(vis, 0, sizeof(vis));
  priority_queue<pair<pair<int, int>, pair<int, int>>> pq;
  for (auto p : v[0]) {
    pq.push({{-(p.first + p.second), 0}, p});
    dis[p.first][p.second] = 0;
  }
  vector<int> cnt(p);
  while (!pq.empty()) {
    int ux = pq.top().second.first;
    int uy = pq.top().second.second;
    int d = -pq.top().first.first;
    int idx = pq.top().first.second;
    pq.pop();
    if (idx == p - 1) {
      cout << d << "\n";
      return 0;
    }
    if (vis[ux][uy]) continue;
    vis[ux][uy] = 1;
    if (cnt[idx] > 5000) continue;
    cnt[idx]++;
    for (int i = 0; i < v[idx + 1].size(); ++i) {
      int vx = v[idx + 1][i].first, vy = v[idx + 1][i].second;
      int c = d + abs(vx - ux) + abs(vy - uy);
      if (dis[vx][vy] > c) {
        dis[vx][vy] = c;
        pq.push({{-dis[vx][vy], idx + 1}, {vx, vy}});
      }
    }
  }
  return 0;
}
