#include <bits/stdc++.h>
using namespace std;
const int Maxn = 301, Mo = 1e9 + 7, INF = INT_MAX;
int n, m, p;
vector<int> xx[Maxn * Maxn], yy[Maxn * Maxn];
vector<int> w[Maxn];
int d[Maxn][Maxn], a[Maxn][Maxn];
string st;
int main() {
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      xx[a[i][j]].push_back(i);
      yy[a[i][j]].push_back(j);
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) d[i][j] = INF;
  for (int i = 1; i <= p; i++) {
    for (int j = 0; j < xx[i].size(); j++) {
      int x = xx[i][j], y = yy[i][j];
      if (i == 1) {
        d[x][y] = abs(x - 1) + abs(y - 1);
      } else {
        for (int k = 1; k <= n; k++) {
          int pos = lower_bound(w[k].begin(), w[k].end(), y) - w[k].begin();
          int len = w[k].size();
          if (pos < len && pos >= 0) {
            int py = w[k][pos];
            d[x][y] = min(d[x][y], d[k][py] + abs(x - k) + abs(y - py));
          }
          if (pos + 1 < len) {
            int py = w[k][pos + 1];
            d[x][y] = min(d[x][y], d[k][py] + abs(x - k) + abs(y - py));
          }
          if (pos - 1 >= 0 && pos - 1 < len) {
            int py = w[k][pos - 1];
            d[x][y] = min(d[x][y], d[k][py] + abs(x - k) + abs(y - py));
          }
        }
      }
    }
    for (int j = 1; j <= n; j++) w[j].clear();
    for (int j = 0; j < xx[i].size(); j++) {
      int x = xx[i][j], y = yy[i][j];
      w[x].push_back(y);
    }
    for (int j = 1; j <= n; j++) sort(w[j].begin(), w[j].end());
  }
  int ans = INF;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == p) ans = min(ans, d[i][j]);
  cout << ans << endl;
}
