#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, m, p;
int dist[90055];
vector<int> vec[90055];
int main() {
  scanf("%d %d %d", &n, &m, &p);
  int x;
  vec[0].emplace_back(0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &x);
      vec[x].emplace_back(i * m + j);
    }
  }
  memset(dist, 63, sizeof(dist));
  dist[0] = 0;
  for (int level = 0; level < p; level++) {
    if (level == 1 && find(vec[1].begin(), vec[1].end(), 0) == vec[1].end())
      dist[0] = (int)1e9;
    if (vec[level + 1].size() * vec[level].size() <= 90000) {
      for (auto u : vec[level]) {
        int x = u / m, y = u % m;
        for (auto v : vec[level + 1]) {
          int nx = v / m, ny = v % m;
          dist[v] = min(dist[v], dist[u] + abs(x - nx) + abs(y - ny));
        }
      }
      continue;
    }
    vector<bool> in_que(n * m, false);
    vector<int> d(n * m, (int)1e9);
    queue<int> que;
    for (auto u : vec[level]) {
      d[u] = dist[u];
      que.push(u);
    }
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      in_que[u] = false;
      int x = u / m, y = u % m;
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        int v = nx * m + ny, w = abs(nx - x) + abs(ny - y);
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && d[v] > d[u] + w) {
          d[v] = d[u] + w;
          if (!in_que[v]) {
            in_que[v] = true;
            que.push(v);
          }
        }
      }
    }
    for (auto u : vec[level + 1]) dist[u] = d[u];
  }
  int res = INT_MAX;
  for (auto u : vec[p]) res = min(res, dist[u]);
  printf("%d\n", res);
  return 0;
}
