#include <bits/stdc++.h>
using namespace std;
int i, j, n, h, x, y, cur_h, k, m, p, fx, fy;
int a[505][505], dp[505][505], d[505][505];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
vector<pair<int, int> > g[250505];
vector<pair<int, pair<int, int> > > lst, bfs;
int Abs(int x) { return x > 0 ? x : -x; }
int find_dist(int x1, int y1, int x2, int y2) {
  return Abs(x1 - x2) + Abs(y1 - y2);
}
bool in_range(int x, int y) { return (x >= 0 && x < y); }
int main() {
  cin >> n >> m >> p;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) dp[i][j] = (int)1e+9;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      g[a[i][j]].push_back(make_pair(i, j));
      if (a[i][j] == 1) dp[i][j] = i + j;
      if (a[i][j] == p) fx = i, fy = j;
    }
  for (i = 2; i <= p; i++) {
    int cur_size = g[i].size();
    int last_size = g[i - 1].size();
    if (cur_size * last_size <= n * m) {
      for (j = 0; j < cur_size; j++) {
        int cur_x = g[i][j].first;
        int cur_y = g[i][j].second;
        for (k = 0; k < last_size; k++) {
          int last_x = g[i - 1][k].first;
          int last_y = g[i - 1][k].second;
          dp[cur_x][cur_y] =
              min(dp[cur_x][cur_y],
                  dp[last_x][last_y] + find_dist(cur_x, cur_y, last_x, last_y));
        }
      }
    } else {
      for (k = 0; k < n; k++)
        for (j = 0; j < m; j++) d[k][j] = -1;
      bfs.clear();
      lst.clear();
      for (j = 0; j < last_size; j++) {
        int last_x = g[i - 1][j].first;
        int last_y = g[i - 1][j].second;
        lst.push_back(make_pair(dp[last_x][last_y], make_pair(last_x, last_y)));
      }
      sort(lst.begin(), lst.end());
      int pointer = 1;
      j = 0;
      bfs.push_back(lst[0]);
      d[lst[0].second.first][lst[0].second.second] = lst[0].first;
      while (j < bfs.size()) {
        int x = bfs[j].second.first;
        int y = bfs[j].second.second;
        int val = bfs[j].first;
        j++;
        while (pointer < lst.size() && lst[pointer].first <= val)
          bfs.push_back(lst[pointer++]);
        for (k = 0; k < 4; k++)
          if (in_range(x + dir[k][0], n) && in_range(y + dir[k][1], m) &&
              d[x + dir[k][0]][y + dir[k][1]] == -1) {
            d[x + dir[k][0]][y + dir[k][1]] = val + 1;
            bfs.push_back(
                make_pair(val + 1, make_pair(x + dir[k][0], y + dir[k][1])));
          }
      }
      for (j = 0; j < cur_size; j++) {
        int cur_x = g[i][j].first;
        int cur_y = g[i][j].second;
        dp[cur_x][cur_y] = d[cur_x][cur_y];
      }
    }
  }
  cout << dp[fx][fy] << endl;
  return 0;
}
