#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int arr[512][512], cnt[102400];
int dp[512][512], dis[512][512];
vector<pair<int, int> > pos[102400];
int main() {
  int n, m, p, enx, eny;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", arr[i] + j);
      if (arr[i][j] == p) {
        enx = i;
        eny = j;
      }
      cnt[arr[i][j]]++;
      pos[arr[i][j]].push_back(make_pair(i, j));
    }
  }
  memset(dp, 0x3F, sizeof(dp));
  for (int i = 0; i < pos[1].size(); i++)
    dp[pos[1][i].first][pos[1][i].second] = pos[1][i].first + pos[1][i].second;
  for (int i = 2; i <= p; i++) {
    if (cnt[i] * cnt[i - 1] <= n * m) {
      for (int j = 0; j < pos[i - 1].size(); j++) {
        for (int k = 0; k < pos[i].size(); k++) {
          dp[pos[i][k].first][pos[i][k].second] =
              min(dp[pos[i][k].first][pos[i][k].second],
                  dp[pos[i - 1][j].first][pos[i - 1][j].second] +
                      ((pos[i][k].first - pos[i - 1][j].first) < 0
                           ? -(pos[i][k].first - pos[i - 1][j].first)
                           : (pos[i][k].first - pos[i - 1][j].first)) +
                      ((pos[i][k].second - pos[i - 1][j].second) < 0
                           ? -(pos[i][k].second - pos[i - 1][j].second)
                           : (pos[i][k].second - pos[i - 1][j].second)));
        }
      }
      continue;
    }
    vector<pair<int, pair<int, int> > > app;
    memset(dis, 0x3F, sizeof(dis));
    for (int j = 0; j < pos[i - 1].size(); j++) {
      app.push_back(make_pair(dp[pos[i - 1][j].first][pos[i - 1][j].second],
                              pos[i - 1][j]));
      dis[pos[i - 1][j].first][pos[i - 1][j].second] =
          dp[pos[i - 1][j].first][pos[i - 1][j].second];
    }
    sort(app.begin(), app.end());
    queue<pair<int, int> > que;
    for (int j = 0; j < app.size(); j++) que.push(app[j].second);
    while (!que.empty()) {
      int x = que.front().first, y = que.front().second;
      que.pop();
      for (int j = 0; j < 4; j++) {
        int newx = x + dx[j], newy = y + dy[j];
        if (newx < 0 || newx >= n || newy < 0 || newy >= m ||
            dis[x][y] + 1 >= dis[newx][newy])
          continue;
        dis[newx][newy] = dis[x][y] + 1;
        que.push(make_pair(newx, newy));
      }
    }
    for (int j = 0; j < pos[i].size(); j++)
      dp[pos[i][j].first][pos[i][j].second] =
          dis[pos[i][j].first][pos[i][j].second];
  }
  printf("%d\n", dp[enx][eny]);
  return 0;
}
