#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
template <class T>
inline T readint(T& a) {
  a = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    a = (a << 3) + (a << 1) + c - '0';
    c = getchar();
  }
  return a;
}
struct BIT {
  int v[310][310];
  int h[310][310];
  BIT() { memset(h, 0, sizeof h); }
  void update(const int& a, const int& b, const int& val, const int& d) {
    for (int i = a; i < 310; i += i & -i)
      for (int j = b; j < 310; j += j & -j) {
        if (h[i][j] != d) h[i][j] = d, v[i][j] = INF;
        v[i][j] = min(v[i][j], val);
      }
  }
  int query(const int& a, const int& b, const int& d) {
    int ans = INF;
    for (int i = a; i; i -= i & -i)
      for (int j = b; j; j -= j & -j) {
        if (h[i][j] == d) ans = min(v[i][j], ans);
      }
    return ans;
  }
} Tree[4];
vector<pair<int, int> > V[90010];
int d[310][310], n, m;
void up(const int& i, const int& j, const int& h) {
  Tree[0].update(i, j, d[i][j] - i - j, h);
  Tree[1].update(i, m + 1 - j, d[i][j] - i + j, h);
  Tree[2].update(n + 1 - i, j, d[i][j] + i - j, h);
  Tree[3].update(n + 1 - i, m + 1 - j, d[i][j] + i + j, h);
}
int qu(const int& i, const int& j, const int& h) {
  int& ans = d[i][j];
  ans = INF;
  ans = min(ans, Tree[0].query(i, j, h - 1) + i + j);
  ans = min(ans, Tree[1].query(i, m + 1 - j, h - 1) + i - j);
  ans = min(ans, Tree[2].query(n + 1 - i, j, h - 1) - i + j);
  ans = min(ans, Tree[3].query(n + 1 - i, m + 1 - j, h - 1) - i - j);
  return ans;
}
int main() {
  int p, a;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1, j; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      readint(a);
      V[a].push_back(make_pair(i, j));
      if (a <= 1) {
        d[i][j] = i + j - 2;
        up(i, j, 1);
      }
    }
  }
  for (int i = 2; i <= p; ++i) {
    for (int j = 0; j < V[i].size(); ++j) {
      qu(V[i][j].first, V[i][j].second, i);
    }
    for (int j = 0; j < V[i].size(); ++j) {
      up(V[i][j].first, V[i][j].second, i);
    }
  }
  printf("%d", d[V[p][0].first][V[p][0].second]);
  return 0;
}
