#include <bits/stdc++.h>
using namespace std;
vector<int> Adj[301];
vector<int> Col[301];
vector<pair<int, int> > To[301 * 301];
inline int dist(pair<int, int> A, pair<int, int> B) {
  return abs(A.first - B.first) + abs(A.second - B.second);
}
int D[301][301];
const int INF = (int)1e9;
int main() {
  int N, M, i, j, P, x;
  scanf("%d%d%d", &N, &M, &P);
  for (i = 0; i < N; ++i)
    for (j = 0; j < M; ++j) {
      scanf("%d", &x);
      To[x].push_back({i, j});
      D[i][j] = INF;
    }
  pair<int, int> z = *To[P].begin();
  D[z.first][z.second] = 0;
  Adj[z.first].push_back(z.second);
  Col[z.second].push_back(z.first);
  for (x = P - 1; x >= 1; --x) {
    for (auto u : To[x]) {
      for (i = 0; i < N; ++i) {
        auto a = lower_bound(Adj[i].begin(), Adj[i].begin(), u.second);
        if (a != Adj[i].end())
          D[u.first][u.second] =
              min(D[u.first][u.second], D[i][*a] + dist(u, {i, *a}));
        if (a != Adj[i].begin()) {
          --a;
          D[u.first][u.second] =
              min(D[u.first][u.second], D[i][*a] + dist(u, {i, *a}));
        }
      }
      for (i = 0; i < M; ++i) {
        auto a = lower_bound(Col[i].begin(), Col[i].end(), u.first);
        if (a != Col[i].end())
          D[u.first][u.second] =
              min(D[u.first][u.second], D[*a][i] + dist(u, {*a, i}));
        if (a != Col[i].begin()) {
          --a;
          D[u.first][u.second] =
              min(D[u.first][u.second], D[*a][i] + dist(u, {*a, i}));
        }
      }
    }
    for (i = 0; i < N; ++i) Adj[i].clear();
    for (i = 0; i < M; ++i) Col[i].clear();
    for (auto v : To[x]) {
      Col[v.second].push_back(v.first);
      Adj[v.first].push_back(v.second);
    }
  }
  int ans = INF;
  for (auto v : To[1]) ans = min(ans, D[v.first][v.second] + dist(v, {0, 0}));
  printf("%d\n", ans);
  return 0;
}
