#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > V[310 * 310];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int a[310][310];
int dp[310][310];
int dis[310][310];
int vis[310][310];
int main() {
  int n, m, p;
  cin >> n >> m >> p;
  memset(dp, (127), sizeof(dp));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      V[a[i][j]].push_back(make_pair(i, j));
      if (a[i][j] == 1) dp[i][j] = i - 1 + j - 1;
    }
  queue<pair<int, int> > Q;
  int level = sqrt(n * m);
  for (int i = 1; i < p; i++) {
    if (V[i].size() <= level) {
      for (int j = 0; j < V[i + 1].size(); j++) {
        for (int k = 0; k < V[i].size(); k++) {
          dp[V[i + 1][j].first][V[i + 1][j].second] =
              min(dp[V[i + 1][j].first][V[i + 1][j].second],
                  dp[V[i][k].first][V[i][k].second] +
                      abs(V[i + 1][j].first - V[i][k].first) +
                      abs(V[i + 1][j].second - V[i][k].second));
        }
      }
    } else {
      memset(dis, (127), sizeof(dis));
      for (int j = 0; j < V[i].size(); j++) {
        dis[V[i][j].first][V[i][j].second] = dp[V[i][j].first][V[i][j].second];
        Q.push(V[i][j]);
      }
      while (!Q.empty()) {
        pair<int, int> now = Q.front();
        Q.pop();
        pair<int, int> next;
        for (int k = 0; k < 4; k++) {
          next.first = now.first + dx[k];
          next.second = now.second + dy[k];
          if (next.first > n || next.first < 1) continue;
          if (next.second > m || next.second < 1) continue;
          if (dis[next.first][next.second] > dis[now.first][now.second] + 1) {
            dis[next.first][next.second] = dis[now.first][now.second] + 1;
            Q.push(next);
          }
        }
      }
      for (int j = 0; j < V[i + 1].size(); j++) {
        dp[V[i + 1][j].first][V[i + 1][j].second] =
            dis[V[i + 1][j].first][V[i + 1][j].second];
      }
    }
  }
  printf("%d\n", dp[V[p][0].first][V[p][0].second]);
  return 0;
}
