#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <class T>
T gcd(T a, T b) {
  return a ? gcd(b % a, a) : b;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T sgn(T a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
int n;
int m;
const int N = 310;
vector<pair<int, pair<int, int>>> t[N * N];
pair<int, pair<int, int>> d[N][N];
bool was[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int p;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x;
      scanf("%d", &x);
      if (x == 1)
        t[x].push_back(make_pair(i + j, make_pair(i, j)));
      else
        t[x].push_back(make_pair(2e9, make_pair(i, j)));
    }
  for (int k = 1; k < p; k++) {
    if ((long long)((int)(t[k]).size()) * ((int)(t[k + 1]).size()) >= n * m) {
      int l = 0;
      vector<pair<int, int>> q;
      memset(d, 127, sizeof(d));
      memset(was, false, sizeof(was));
      sort((t[k]).begin(), (t[k]).end());
      int j = 0;
      q.push_back(t[k][j].second),
          d[t[k][j].second.first][t[k][j].second.second] = t[k][j];
      j++;
      while (l < ((int)(q).size())) {
        pair<int, int> cur = q[l++];
        while (j < ((int)(t[k]).size()) &&
               d[cur.first][cur.second].first + 1 >= t[k][j].first) {
          q.push_back(t[k][j].second);
          d[t[k][j].second.first][t[k][j].second.second] = t[k][j];
          j++;
        }
        if (was[cur.first][cur.second]) continue;
        was[cur.first][cur.second] = true;
        for (int i = 0; i < 4; i++) {
          int x = cur.first + dx[i];
          int y = cur.second + dy[i];
          if (x < 0 || y < 0 || x >= n || y >= m) continue;
          if (d[x][y].first > d[cur.first][cur.second].first + 1) {
            d[x][y] = make_pair(d[cur.first][cur.second].first + 1, cur);
            q.push_back(make_pair(x, y));
          }
        }
      }
      for (auto& w : t[k + 1])
        w.first = d[w.second.first][w.second.second].first;
    } else {
      for (auto w1 : t[k])
        for (auto& w2 : t[k + 1]) {
          w2.first = min(w1.first + abs(w1.second.first - w2.second.first) +
                             abs(w1.second.second - w2.second.second),
                         w2.first);
        }
    }
  }
  int ans = 2e9;
  for (auto w : t[p]) ans = min(w.first, ans);
  cout << ans << endl;
  return 0;
}
