#include <bits/stdc++.h>
using namespace std;
int ar[305][305];
vector<int> col[305];
int dp[305][305];
vector<pair<int, int> > poss[90005];
bool vis[305];
int n, m, p;
void pr() {}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      scanf("%d", &ar[i][j]);
      if (ar[i][j] == 1)
        dp[i][j] = j + i;
      else
        dp[i][j] = 1e9;
      poss[ar[i][j]].push_back(make_pair(i, j));
    }
  pr();
  for (int val = 1; val <= p - 1; ++val) {
    for (int i = 0; i < max(m, n); ++i) {
      col[i].clear();
      vis[i] = false;
    }
    for (pair<int, int> p : poss[val + 1]) {
      int i = p.first;
      int j = p.second;
      col[j].push_back(i);
    }
    for (pair<int, int> p : poss[val]) vis[p.first] = true;
    for (int y = 0; y < n; ++y)
      if (vis[y]) {
        int best = 1e9;
        for (int x = 0; x < m; ++x) {
          ++best;
          if (ar[y][x] == val) best = min(best, dp[y][x]);
          for (int he : col[x]) dp[he][x] = min(dp[he][x], best + abs(he - y));
        }
        best = 1e9;
        for (int x = m - 1; x >= 0; --x) {
          ++best;
          if (ar[y][x] == val) best = min(best, dp[y][x]);
          for (int he : col[x]) dp[he][x] = min(dp[he][x], best + abs(he - y));
        }
      }
    pr();
  }
  for (int x = 0; x < m; ++x)
    for (int y = 0; y < n; ++y)
      if (ar[y][x] == p) printf("%d\n", dp[y][x]);
  return 0;
}
