#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e2 + 10;
int n, m, p, a[maxn][maxn];
int dp[maxn][maxn], dis[maxn][maxn];
queue<pair<int, int> > q;
vector<pair<int, int> > vc[maxn * maxn], temp[2];
bool cmp(pair<int, int> x, pair<int, int> y) {
  return dp[x.first][x.second] < dp[y.first][y.second];
}
bool vis[maxn][maxn];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void bfs(int c, int nc) {
  while (q.size()) q.pop();
  memset(dis, 0x3f, sizeof dis);
  memset(vis, 0, sizeof vis);
  sort(vc[c].begin(), vc[c].end(), cmp);
  for (auto it : vc[c]) dis[it.first][it.second] = dp[it.first][it.second];
  pair<int, int> it = vc[c][0];
  dis[it.first][it.second] = dp[it.first][it.second];
  vis[it.first][it.second] = 1;
  q.push(it);
  int sz = vc[c].size();
  int pos = 1;
  while (q.size() || pos < sz) {
    if (pos < sz)
      while (vis[vc[c][pos].first][vc[c][pos].second] && pos < sz) pos++;
    if (q.size() == 0 && pos < sz)
      q.push(vc[c][pos++]),
          vis[vc[c][pos - 1].first][vc[c][pos - 1].second] = 1;
    if (q.size() == 0) break;
    int x = q.front().first, y = q.front().second;
    q.pop();
    while (pos < sz) {
      if (dis[x][y] >= dis[vc[c][pos].first][vc[c][pos].second] - 1) {
        if (!vis[vc[c][pos].first][vc[c][pos].second])
          q.push(vc[c][pos]), vis[vc[c][pos].first][vc[c][pos].second] = 1;
        pos++;
      } else
        break;
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + x, ny = dy[i] + y;
      if (nx > n || nx < 1 || ny > m || ny < 1) continue;
      if (!vis[nx][ny]) {
        dis[nx][ny] = min(dis[nx][ny], dis[x][y] + 1);
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
    }
  }
  for (auto it : vc[nc]) dp[it.first][it.second] = dis[it.first][it.second];
}
int main() {
  scanf("%d %d %d", &n, &m, &p);
  memset(dp, 0x3f, sizeof dp);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      vc[a[i][j]].push_back({i, j});
    }
  temp[0].clear();
  temp[0].push_back({1, 1});
  temp[1] = vc[1];
  for (auto i : temp[0])
    for (auto j : temp[1]) {
      int x1 = i.first, y1 = i.second;
      int x2 = j.first, y2 = j.second;
      dp[x2][y2] = min(dp[x2][y2], abs(x2 - x1) + abs(y2 - y1));
    }
  for (int c = 1; c < p; c++) {
    int nc = c + 1;
    if ((int)vc[c].size() * (int)vc[nc].size() >= n * m) {
      bfs(c, nc);
      continue;
    }
    temp[0] = vc[c];
    temp[1] = vc[nc];
    for (auto i : temp[0])
      for (auto j : temp[1]) {
        int x1 = i.first, y1 = i.second;
        int x2 = j.first, y2 = j.second;
        dp[x2][y2] = min(dp[x2][y2], dp[x1][y1] + abs(x2 - x1) + abs(y2 - y1));
      }
  }
  int ans = INT_MAX;
  for (auto it : vc[p]) ans = min(ans, dp[it.first][it.second]);
  printf("%d\n", ans);
  return 0;
}
