#include <bits/stdc++.h>
using namespace std;
int dp[500][500];
int maze[500][500];
vector<pair<int, int> > where[100100];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int mark[500][500];
int main(void) {
  int n, m, p;
  scanf("%d %d %d", &n, &m, &p);
  int fx, fy;
  int ans = 1e8;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = 1000000000;
      scanf("%d", &maze[i][j]);
      where[maze[i][j]].push_back({i, j});
      if (maze[i][j] == p) {
        dp[i][j] = 0;
        fx = i, fy = j;
        if (p == 1) ans = abs(i - 1) + abs(j - 1);
      }
    }
  }
  for (int i = p - 1; i >= 0; i--) {
    if (max(where[i].size(), where[i + 1].size()) <= sqrt(n * m)) {
      for (int j = 0; j < where[i].size(); j++) {
        int cx = where[i][j].first, cy = where[i][j].second;
        for (int k = 0; k < where[i + 1].size(); k++) {
          int nx = where[i + 1][k].first, ny = where[i + 1][k].second;
          dp[cx][cy] =
              min(dp[cx][cy], dp[nx][ny] + abs(nx - cx) + abs(ny - cy));
        }
        if (i == 1) ans = min(ans, dp[cx][cy] + abs(cx - 1) + abs(cy - 1));
      }
    } else {
      memset(mark, 0, sizeof(mark));
      vector<pair<int, pair<int, int> > > vc;
      for (int j = 0; j < where[i + 1].size(); j++) {
        int cx = where[i + 1][j].first, cy = where[i + 1][j].second;
        vc.push_back({dp[cx][cy], {cx, cy}});
      }
      sort(vc.begin(), vc.end());
      queue<pair<int, pair<int, int> > > q;
      q.push(vc[0]);
      int cnt = 1;
      while (!q.empty()) {
        pair<int, pair<int, int> > cur = q.front();
        q.pop();
        int x = cur.second.first, y = cur.second.second;
        int wt = cur.first;
        if (maze[x][y] == i) {
          dp[x][y] = wt;
        }
        while (cnt < vc.size() && vc[cnt].first <= wt) {
          q.push(vc[cnt]);
          cnt++;
        }
        for (int d = 0; d < 4; d++) {
          int nx = x + dx[d], ny = y + dy[d];
          if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && mark[nx][ny] == 0) {
            mark[nx][ny] = 1;
            q.push({wt + 1, {nx, ny}});
          }
        }
      }
      for (int j = 0; j < where[i].size(); j++) {
        int cx = where[i][j].first, cy = where[i][j].second;
        if (i == 1) ans = min(ans, dp[cx][cy] + abs(cx - 1) + abs(cy - 1));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
