#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p);
    p = (p * p);
  }
  return (T)ret;
}
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int toLlint(string s) {
  long long int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int ts, kk = 1;
int p[301][301];
bool vs[301][301];
int n, m, x, gr[301][301];
int ds[301][301];
vector<pair<int, int> > v[301 * 301];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int t, i, j, k;
  pair<int, int> q;
  scanf("%d%d%d", &n, &m, &x);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &gr[i][j]);
      v[gr[i][j]].push_back(pair<int, int>(i, j));
      if (gr[i][j] == 1)
        p[i][j] = i + j;
      else
        p[i][j] = 1000000007LL;
    }
  }
  for (t = 2; t <= x; t++) {
    if (v[t - 1].size() * v[t].size() > n * m) {
      memset(ds, -1, sizeof(ds));
      queue<int> qu;
      for (i = 0; i < v[t - 1].size(); i++) {
        qu.push(v[t - 1][i].first);
        qu.push(v[t - 1][i].second);
        ds[v[t - 1][i].first][v[t - 1][i].second] =
            p[v[t - 1][i].first][v[t - 1][i].second];
      }
      while (!qu.empty()) {
        int u = qu.front();
        qu.pop();
        int v = qu.front();
        qu.pop();
        for (k = 0; k < 4; k++) {
          int nu = u + dx[k];
          int nv = v + dy[k];
          if (nu >= 0 && nu < n && nv >= 0 && nv < m &&
              (ds[nu][nv] == -1 || ds[nu][nv] > ds[u][v] + 1)) {
            ds[nu][nv] = ds[u][v] + 1;
            qu.push(nu);
            qu.push(nv);
            if (gr[nu][nv] == t) p[nu][nv] = ds[nu][nv];
          }
        }
      }
    } else {
      for (i = 0; i < v[t - 1].size(); i++)
        for (j = 0; j < v[t].size(); j++) {
          p[v[t][j].first][v[t][j].second] =
              min(p[v[t][j].first][v[t][j].second],
                  p[v[t - 1][i].first][v[t - 1][i].second] +
                      abs(v[t - 1][i].first - v[t][j].first) +
                      abs(v[t - 1][i].second - v[t][j].second));
        }
    }
  }
  printf("%d\n", p[v[x][0].first][v[x][0].second]);
  return 0;
}
