#include <bits/stdc++.h>
using namespace std;
const long long inf = ~0ull >> 4;
vector<pair<int, int> > key[90005];
long long ans[405][405];
long long dis[405][405];
int vis[405][405];
int main() {
  int n, m, p, x;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &x);
      key[x].push_back(pair<int, int>(i, j));
      ans[i][j] = inf;
      dis[i][j] = inf;
    }
  }
  for (int i = 1; i <= m; ++i) {
    vis[1][i] = 0;
    dis[1][i] = i - 1;
  }
  for (int i = 1; i <= p; ++i) {
    for (auto& j : key[i]) {
      for (int k = 1; k <= n; ++k) {
        if (vis[k][j.second] == i - 1)
          ans[j.first][j.second] =
              min(ans[j.first][j.second], dis[k][j.second] + abs(k - j.first));
      }
    }
    for (auto& j : key[i]) {
      for (int k = 1; k <= m; ++k) {
        if (vis[j.first][k] != i) dis[j.first][k] = inf;
        dis[j.first][k] =
            min(dis[j.first][k], ans[j.first][j.second] + abs(k - j.second));
        vis[j.first][k] = i;
      }
    }
  }
  printf("%lld\n", ans[key[p][0].first][key[p][0].second]);
}
