#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int N, M, P;
inline int getid(pair<int, int> point) {
  return (point.first - 1) * M + point.second;
}
inline int getDist(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
bool vis[310 * 310];
int dist[310][310], dp[310][310], grid[310][310];
vector<pair<int, int> > typePos[310 * 310];
queue<pair<int, int> > q;
int main() {
  scanf("%d%d%d", &N, &M, &P);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) {
      scanf("%d", &grid[i][j]);
      typePos[grid[i][j]].push_back(pair<int, int>(i, j));
    }
  memset(dp, 0x3f, sizeof dp);
  for (int i = 0; i < (int)typePos[1].size(); i++)
    dp[typePos[1][i].first][typePos[1][i].second] =
        getDist(pair<int, int>(1, 1), typePos[1][i]);
  for (int type = 1; type < P; type++) {
    vector<pair<int, int> > &cur = typePos[type], &nxt = typePos[type + 1];
    if (cur.size() * nxt.size() <= N * M) {
      for (int i = 0; i < (int)cur.size(); i++) {
        for (int j = 0; j < (int)nxt.size(); j++) {
          dp[nxt[j].first][nxt[j].second] =
              min(dp[nxt[j].first][nxt[j].second],
                  dp[cur[i].first][cur[i].second] + getDist(cur[i], nxt[j]));
        }
      }
    } else {
      memset(vis, 0, sizeof vis);
      memset(dist, 0x3f, sizeof dist);
      while (!q.empty()) q.pop();
      for (int i = 0; i < (int)cur.size(); i++) {
        dist[cur[i].first][cur[i].second] = dp[cur[i].first][cur[i].second];
        q.push(cur[i]);
        vis[getid(cur[i])] = true;
      }
      while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        vis[getid(pair<int, int>(x, y))] = false;
        for (int i = 0; i < 4; i++) {
          int nx = x + dx[i], ny = y + dy[i];
          if (1 <= nx && nx <= N && 1 <= ny && ny <= M &&
              dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            if (!vis[getid(pair<int, int>(nx, ny))]) {
              vis[getid(pair<int, int>(nx, ny))] = true;
              q.push(pair<int, int>(nx, ny));
            }
          }
        }
      }
      for (int i = 0; i < (int)nxt.size(); i++)
        dp[nxt[i].first][nxt[i].second] = dist[nxt[i].first][nxt[i].second];
    }
  }
  printf("%d\n", dp[typePos[P][0].first][typePos[P][0].second]);
  return 0;
}
