#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const int N = 305;
const int M = 1e9 + 7;
int n, m, k, p;
int d[N][N];
vector<vector<pair<int, int>>> vec;
vector<pair<int, int>> dist[3][N];
inline int getDIST(int x, int y, int xx, int yy) {
  return abs(x - xx) + abs(y - yy);
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> d[i][j];
  vec.resize(p + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) vec[d[i][j]].push_back(make_pair(i, j));
  for (int i = 0; i < vec[1].size(); i++) {
    int x = vec[1][i].first;
    int y = vec[1][i].second;
    int d = getDIST(1, 1, x, y);
    dist[1][y].push_back({d, x});
  }
  int t = 1;
  int nt = 2;
  for (int i = 2; i <= p; i++) {
    for (int j = 1; j <= m; j++) {
      sort(dist[t][j].begin(), dist[t][j].end());
      dist[nt][j].clear();
    }
    for (int h = 0; h < vec[i].size(); h++) {
      int x = vec[i][h].first;
      int y = vec[i][h].second;
      int w = INF;
      for (int j = 1; j <= m; j++) {
        int k = 0;
        int dy = abs(j - y);
        while (k < dist[t][j].size() && w > dist[t][j][k].first + dy) {
          w = min(w, dist[t][j][k].first + abs(dist[t][j][k].second - x) + dy);
          k++;
        }
      }
      dist[nt][y].push_back({w, x});
    }
    swap(t, nt);
  }
  int ans = INF;
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < dist[t][i].size(); j++)
      ans = min(ans, dist[t][i][j].first);
  cout << ans;
  return 0;
}
