#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int n, m, p;
int dp[N][N], visited[N][N], specialdp[N][N];
map<int, vector<pair<int, int> > > color;
struct node {
  int r, c, cost;
  bool operator<(const node &t) const { return cost < t.cost; }
};
bool open(int r, int c) {
  if (r < 1 || c < 1 || r > n || c > m) return false;
  if (visited[r][c]) return false;
  visited[r][c] = true;
  return true;
}
void special(int c) {
  memset(visited, 0, sizeof(visited));
  memset(specialdp, 0x3f, sizeof(specialdp));
  vector<node> initial;
  for (auto a : color[c]) {
    initial.push_back({a.first, a.second, dp[a.first][a.second]});
    visited[a.first][a.second] = true;
  }
  sort(initial.begin(), initial.end());
  int l = 1;
  vector<node> bfs(1, initial[0]);
  for (int i = 0; i < bfs.size(); ++i) {
    node cur = bfs[i];
    while (l < initial.size() && initial[l].cost <= cur.cost + 1) {
      open(initial[l].r, initial[l].c);
      bfs.push_back(initial[l++]);
    }
    specialdp[cur.r][cur.c] = cur.cost;
    if (open(cur.r - 1, cur.c)) bfs.push_back({cur.r - 1, cur.c, cur.cost + 1});
    if (open(cur.r + 1, cur.c)) bfs.push_back({cur.r + 1, cur.c, cur.cost + 1});
    if (open(cur.r, cur.c + 1)) bfs.push_back({cur.r, cur.c + 1, cur.cost + 1});
    if (open(cur.r, cur.c - 1)) bfs.push_back({cur.r, cur.c - 1, cur.cost + 1});
  }
  for (auto a : color[c + 1])
    dp[a.first][a.second] = specialdp[a.first][a.second];
}
int main() {
  memset(dp, 0x3f, sizeof(dp));
  cin >> n >> m >> p;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      int c;
      scanf("%d", &c);
      color[c].push_back({i, j});
      if (c == 1) dp[i][j] = i + j - 2;
    }
  for (int i = 1; i <= p; ++i) {
    if (color[i].size() * color[i + 1].size() > n * n) {
      special(i);
    } else {
      for (auto a : color[i])
        for (auto b : color[i + 1])
          dp[b.first][b.second] =
              min(dp[b.first][b.second], dp[a.first][a.second] +
                                             abs(a.first - b.first) +
                                             abs(a.second - b.second));
    }
  }
  int ans = 1e9;
  for (auto a : color[p]) ans = min(ans, dp[a.first][a.second]);
  cout << ans << '\n';
}
