#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
vector<int> a[300 * 300 + 5];
int ans[300 * 300 + 5];
int dis[300 * 300 + 5];
int vis[300 * 300 + 5];
int main() {
  int n, m, p, x;
  cin >> n >> m >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &x);
      a[x].push_back(i * m + j);
    }
  }
  memset((dis), (0x3f), sizeof((dis)));
  memset((ans), (0x3f), sizeof((ans)));
  for (int i = 0; i < m; i++) {
    dis[i] = i;
    vis[i] = 0;
  }
  for (int i = 1; i <= p; i++) {
    for (auto now : a[i]) {
      int a2 = now / m, b2 = now % m;
      for (int j = 0; j < n; j++) {
        int pre = j * m + b2;
        if (vis[pre] == i - 1) {
          ans[now] = min(ans[now], dis[pre] + abs(j - a2));
        }
      }
    }
    for (auto now : a[i]) {
      int a2 = now / m, b2 = now % m;
      ;
      for (int j = 0; j < m; j++) {
        int ne = a2 * m + j;
        if (vis[ne] != i) {
          vis[ne] = i;
          dis[ne] = ans[now] + abs(b2 - j);
        } else {
          dis[ne] = min(dis[ne], ans[now] + abs(b2 - j));
        }
      }
    }
  }
  cout << ans[a[p][0]] << endl;
  return 0;
}
