#include <bits/stdc++.h>
using namespace std;
const int MAX = 300 + 5;
int n, m, p;
int grid[MAX][MAX];
int dp[MAX][MAX];
vector<pair<int, int> > pos[MAX * MAX];
vector<int> col[MAX];
int main() {
  scanf("%d %d %d", &n, &m, &p);
  for (int i = int(0); i < int(n); i++) {
    for (int j = int(0); j < int(m); j++) {
      scanf("%d", &grid[i][j]);
      if (grid[i][j] == 1) {
        dp[i][j] = i + j;
      } else {
        dp[i][j] = 1 << 30;
      }
      pos[grid[i][j]].emplace_back(i, j);
    }
  }
  bool visited[MAX];
  for (int i = int(1); i < int(p); i++) {
    memset(visited, false, sizeof visited);
    for (int c = int(0); c < int(m); c++) {
      col[c].clear();
    }
    for (auto &each : pos[i + 1]) {
      col[each.second].push_back(each.first);
    }
    for (auto &each : pos[i]) {
      visited[each.first] = true;
    }
    for (int r = int(0); r < int(n); r++) {
      if (visited[r]) {
        int best = 1 << 30;
        for (int c = int(0); c < int(m); c++) {
          best++;
          if (grid[r][c] == i) {
            best = min(best, dp[r][c]);
          }
          for (auto &each : col[c]) {
            dp[each][c] = min(dp[each][c], best + abs(each - r));
          }
        }
        best = 1 << 30;
        for (int c = int(m - 1); c >= int(0); c--) {
          best++;
          if (grid[r][c] == i) {
            best = min(best, dp[r][c]);
          }
          for (auto &each : col[c]) {
            dp[each][c] = min(dp[each][c], best + abs(each - r));
          }
        }
      }
    }
  }
  printf("%d\n", dp[pos[p][0].first][pos[p][0].second]);
  return 0;
}
