#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[200005];
int dist[305][305], arr[305][305], n, m, di[305][305];
queue<pair<int, int> > q;
vector<pair<int, pair<int, int> > > v;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int distValue(pair<int, int> p1, pair<int, int> p2) {
  return abs(p1.first - p2.first) + abs(p1.second - p2.second);
}
bool valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); }
int main() {
  int k, i, j, p, X, Y;
  scanf("%d %d %d", &n, &m, &p);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dist[i][j] = 2000000200;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%d", &arr[i][j]);
      if (arr[i][j] == p) {
        X = i;
        Y = j;
      }
      if (arr[i][j] == 1) {
        dist[i][j] = (i + j - 2);
      }
      adj[arr[i][j]].push_back(make_pair(i, j));
    }
  }
  for (i = 2; i <= p; i++) {
    int curr = adj[i].size(), pre = adj[i - 1].size();
    if (curr * pre <= n * m) {
      for (auto p1 : adj[i - 1]) {
        for (auto p2 : adj[i]) {
          dist[p2.first][p2.second] =
              min(dist[p2.first][p2.second],
                  dist[p1.first][p1.second] + distValue(p1, p2));
        }
      }
    } else {
      v.clear();
      for (j = 1; j <= n; j++) {
        for (k = 1; k <= m; k++) {
          di[j][k] = 2000000200;
        }
      }
      for (auto p1 : adj[i - 1]) {
        v.push_back(make_pair(dist[p1.first][p1.second], p1));
        di[p1.first][p1.second] = dist[p1.first][p1.second];
      }
      sort(v.begin(), v.end());
      int pos = 1;
      q.push(v[0].second);
      while (!q.empty() || pos < v.size()) {
        if (q.empty()) {
          q.push(v[pos].second);
          pos++;
        }
        pair<int, int> p1 = q.front();
        q.pop();
        while (pos < v.size() && v[pos].first <= di[p1.first][p1.second]) {
          q.push(v[pos].second);
          pos++;
        }
        for (j = 0; j < 4; j++) {
          if (valid(p1.first + dir[j][0], p1.second + dir[j][1]) &&
              di[p1.first + dir[j][0]][p1.second + dir[j][1]] >
                  (di[p1.first][p1.second] + 1)) {
            di[p1.first + dir[j][0]][p1.second + dir[j][1]] =
                di[p1.first][p1.second] + 1;
            q.push(make_pair(p1.first + dir[j][0], p1.second + dir[j][1]));
          }
        }
      }
      for (auto p1 : adj[i]) {
        dist[p1.first][p1.second] = di[p1.first][p1.second];
      }
    }
  }
  printf("%d\n", dist[X][Y]);
  return 0;
}
