#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200001;
int dp[305][305], a[305][305];
int dist[305][305];
int n, m, k;
vector<pair<int, int> > v[90001];
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
struct Node {
  int y, x, v;
  bool operator<(const Node &nd) const { return v > nd.v; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 305; i++)
    for (int j = 0; j < 305; j++) dp[i][j] = 2e9;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 1) dp[i][j] = i + j;
      v[a[i][j]].push_back({i, j});
    }
  for (int i = 2; i <= k; i++) {
    if (v[i - 1].size() * v[i].size() > 5 * n * m) {
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          dist[i][j] = 2e9;
        }
      priority_queue<Node> pq;
      for (auto &p : v[i - 1]) {
        int y = p.first;
        int x = p.second;
        dist[y][x] = dp[y][x];
        pq.push({y, x, dist[y][x]});
      }
      while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        int y = p.y;
        int x = p.x;
        int v = p.v;
        if (v > dist[y][x]) continue;
        for (int k = 0; k < 4; k++) {
          int ny = y + dy[k];
          int nx = x + dx[k];
          if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
          if (dist[ny][nx] > dist[y][x] + 1) {
            dist[ny][nx] = dist[y][x] + 1;
            pq.push({ny, nx, dist[ny][nx]});
          }
        }
      }
      for (auto &p : v[i]) {
        int y = p.first;
        int x = p.second;
        dp[y][x] = dist[y][x];
      }
    } else {
      for (auto &p1 : v[i - 1]) {
        int y1 = p1.first;
        int x1 = p1.second;
        for (auto p2 : v[i]) {
          int y2 = p2.first;
          int x2 = p2.second;
          dp[y2][x2] =
              min(dp[y2][x2], dp[y1][x1] + abs(y1 - y2) + abs(x1 - x2));
        }
      }
    }
  }
  int ans = 2e9;
  for (auto &p : v[k]) {
    int y = p.first;
    int x = p.second;
    ans = min(ans, dp[y][x]);
  }
  cout << ans;
  return 0;
}
