#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > Q;
vector<pair<int, int> > T[301 * 301];
int R, C, P, A[301][301], D[301][301], RM[4] = {0, 0, 1, -1},
                                       CM[4] = {1, -1, 0, 0};
bool valid(int r, int c) { return 0 <= r && r < R && 0 <= c && c < C; }
bool cmp(const pair<int, int>& A, const pair<int, int>& B) {
  return D[A.first][A.second] < D[B.first][B.second];
}
void bfs(int p) {
  int dist[301][301], K = 0;
  memset(dist, -1, sizeof(dist));
  sort(T[p].begin(), T[p].end(), cmp);
  while (true) {
    if (K < T[p].size()) {
      Q.push({T[p][K].first, T[p][K].second});
      dist[Q.front().first][Q.front().second] =
          D[Q.front().first][Q.front().second];
    }
    while (K < T[p].size() && D[T[p][K].first][T[p][K].second] ==
                                  D[Q.front().first][Q.front().second]) {
      dist[T[p][K].first][T[p][K].second] = D[T[p][K].first][T[p][K].second];
      Q.push({T[p][K].first, T[p][K].second});
      K++;
    }
    if (Q.empty()) break;
    while (!Q.empty()) {
      int r = Q.front().first;
      int c = Q.front().second;
      Q.pop();
      for (int k = 0; k < (int)4; k++) {
        int rr = r + RM[k];
        int cc = c + CM[k];
        if (valid(rr, cc) && dist[rr][cc] == -1 && A[rr][cc] != p) {
          dist[rr][cc] = dist[r][c] + 1;
          Q.push({rr, cc});
        }
      }
      while (K < T[p].size() &&
             D[T[p][K].first][T[p][K].second] == dist[r][c] + 1) {
        dist[T[p][K].first][T[p][K].second] = D[T[p][K].first][T[p][K].second];
        Q.push({T[p][K].first, T[p][K].second});
        K++;
      }
    }
  }
  for (int r = 0; r < (int)R; r++)
    for (int c = 0; c < (int)C; c++)
      if (A[r][c] == p + 1) D[r][c] = dist[r][c];
}
int main() {
  scanf("%d %d\n", &R, &C);
  scanf("%d\n", &P);
  for (int r = 0; r < (int)R; r++)
    for (int c = 0; c < (int)C; c++) {
      scanf("%d\n", &A[r][c]);
      T[A[r][c]].push_back({r, c});
      D[r][c] = int(1e9);
    }
  for (int i = 0; i < (int)T[1].size(); i++) {
    int r = T[1][i].first;
    int c = T[1][i].second;
    D[r][c] = r + c;
  }
  for (int p = 1; p < (int)P; p++) {
    if (T[p].size() * T[p + 1].size() >= R * C) {
      bfs(p);
    } else {
      for (int i = 0; i < (int)T[p].size(); i++) {
        int r1 = T[p][i].first;
        int c1 = T[p][i].second;
        for (int j = 0; j < (int)T[p + 1].size(); j++) {
          int r2 = T[p + 1][j].first;
          int c2 = T[p + 1][j].second;
          D[r2][c2] = min(D[r2][c2], D[r1][c1] + abs(r1 - r2) + abs(c1 - c2));
        }
      }
    }
  }
  printf("%d\n", D[T[P][0].first][T[P][0].second]);
}
