#include <bits/stdc++.h>
using namespace std;
const int MAXN = 310;
const int INF = 1e9;
const int S = 90000;
int n, m, p;
int mp[MAXN][MAXN];
int dist[MAXN][MAXN];
vector<pair<int, int>> g[MAXN][MAXN];
vector<pair<int, int>> fsz[MAXN * MAXN];
int pr(int i, int j) {
  if ((0 <= i) && (i <= n) && (0 <= j) && (j <= m)) {
    return true;
  } else {
    return false;
  }
}
void dijk(int c) {
  if (fsz[c].size() * fsz[c - 1].size() > S) {
    int min_zn = INF;
    for (auto e : fsz[c - 1]) {
      min_zn = min(min_zn, dist[e.first][e.second]);
    }
    for (auto e : fsz[c - 1]) {
      dist[e.first][e.second] -= min_zn;
    }
    vector<pair<int, int>> q[2 * MAXN];
    for (auto e : fsz[c - 1]) {
      if (dist[e.first][e.second] < n + m)
        q[dist[e.first][e.second]].push_back(e);
    }
    for (int i = 0; i < n + m; ++i) {
      for (auto e1 : q[i]) {
        for (auto e2 : g[e1.first][e1.second]) {
          if (dist[e1.first][e1.second] + 1 < dist[e2.first][e2.second]) {
            q[dist[e1.first][e1.second] + 1].push_back(e2);
            dist[e2.first][e2.second] = dist[e1.first][e1.second] + 1;
          }
        }
      }
    }
    for (int j = 0; j < n; ++j) {
      for (int l = 0; l < m; ++l) {
        if (mp[j][l] != c) {
          dist[j][l] = INF;
        } else {
          dist[j][l] += min_zn;
        }
      }
    }
  } else {
    for (auto e1 : fsz[c - 1]) {
      for (auto e2 : fsz[c]) {
        dist[e2.first][e2.second] =
            min(dist[e2.first][e2.second], dist[e1.first][e1.second] +
                                               abs(e2.first - e1.first) +
                                               abs(e2.second - e1.second));
      }
    }
    for (auto e1 : fsz[c - 1]) {
      dist[e1.first][e1.second] = INF;
    }
    if ((c == 1) && (fsz[1][0] == make_pair(0, 0))) {
      dist[0][0] = 0;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m >> p;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> mp[i][j];
      dist[i][j] = INF;
      fsz[mp[i][j]].push_back({i, j});
    }
  }
  fsz[0].push_back({0, 0});
  dist[0][0] = 0;
  vector<pair<int, int>> fg = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (auto e : fg) {
        if (pr(i + e.first, j + e.second)) {
          g[i][j].push_back({i + e.first, j + e.second});
        }
      }
    }
  }
  for (int i = 1; i <= p; ++i) {
    dijk(i);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (mp[i][j] == p) {
        cout << dist[i][j] << endl;
        return 0;
      }
    }
  }
  return 0;
}
