#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
int di[] = {1, 0, -1, 0}, dj[] = {0, 1, 0, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, x;
  cin >> m >> n >> x;
  vector<vector<int>> a(m, vector<int>(n));
  vector<vector<pair<int, int>>> b(x + 1);
  for (int i = (0); i < (m); i++)
    for (int j = (0); j < (n); j++) {
      cin >> a[i][j];
      b[a[i][j]].emplace_back(i, j);
    }
  vector<vector<int>> d(m, vector<int>(n, oo));
  for (const auto &p : b[1]) d[p.first][p.second] = p.first + p.second;
  for (int k = (1); k < (x); k++) {
    cerr << k << endl;
    if (int((b[k]).size()) > 1000) {
      vector<vector<int>> dd(m, vector<int>(n, oo));
      set<pair<int, pair<int, int>>> q;
      for (const auto &p : b[k]) {
        int i, j;
        tie(i, j) = p;
        dd[i][j] = d[i][j], q.insert(make_pair(d[i][j], p));
      }
      while (int((q).size())) {
        int i, j;
        tie(i, j) = begin(q)->second;
        q.erase(begin(q));
        for (int t = (0); t < (4); t++) {
          int ni = i + di[t], nj = j + dj[t];
          if (ni < 0 || ni >= m || nj < 0 || nj >= n) continue;
          if (dd[ni][nj] < dd[i][j] + 1) continue;
          q.erase(make_pair(dd[ni][nj], make_pair(ni, nj)));
          dd[ni][nj] = dd[i][j] + 1;
          q.insert(make_pair(dd[ni][nj], make_pair(ni, nj)));
        }
      }
      for (const auto &p : b[k + 1])
        d[p.first][p.second] = dd[p.first][p.second];
    } else {
      for (const auto &p : b[k]) {
        int i, j;
        tie(i, j) = p;
        for (const auto &np : b[k + 1]) {
          int ni, nj;
          tie(ni, nj) = np;
          d[ni][nj] = min(d[ni][nj], d[i][j] + abs(i - ni) + abs(j - nj));
        }
      }
    }
  }
  int res = oo;
  for (const auto &p : b[x]) res = min(res, d[p.first][p.second]);
  cout << res << endl;
}
