#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, m, x;
long long dis[400][400];
long long d[400][400];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
vector<pair<int, int> > v[N];
bool in(int i, int j) { return 1 <= i && i <= n && 1 <= j && j <= m; }
int main() {
  for (int i = 0; i < 400; i++)
    for (int j = 0; j < 400; j++) dis[i][j] = 1ll << 50;
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int d;
      cin >> d;
      v[d].push_back({i, j});
      if (d == 1) dis[i][j] = i - 1 + j - 1;
    }
  for (int i = 1; i < x; i++) {
    vector<pair<int, int> > a = v[i];
    vector<pair<int, int> > b = v[i + 1];
    if (a.size() * b.size() <= n * m) {
      for (int j = 0; j < a.size(); j++)
        for (int k = 0; k < b.size(); k++)
          dis[b[k].first][b[k].second] =
              min(dis[b[k].first][b[k].second],
                  dis[a[j].first][a[j].second] + abs(a[j].first - b[k].first) +
                      abs(b[k].second - a[j].second));
    } else {
      memset(d, 100, sizeof d);
      queue<pair<int, int> > Q;
      for (auto x : a) d[x.first][x.second] = dis[x.first][x.second];
      for (auto x : a) Q.push(x);
      while (!Q.empty()) {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for (int k = 0; k < 4; k++) {
          int nx = dx[k] + x;
          int ny = dy[k] + y;
          if (!in(nx, ny)) continue;
          if (d[nx][ny] <= 1 + d[x][y]) continue;
          d[nx][ny] = 1 + d[x][y];
          Q.push({nx, ny});
        }
      }
      for (auto x : b) dis[x.first][x.second] = d[x.first][x.second];
    }
  }
  long long ans = dis[v[x][0].first][v[x][0].second];
  cout << ans << endl;
  return 0;
}
