#include <bits/stdc++.h>
using namespace std;
int n, m, p, k, a;
vector<pair<int, int> > G[90002];
int dp[2][302][302];
int main() {
  memset(dp[1], -1, sizeof(dp[1]));
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a;
      G[a].push_back({i, j});
      if (a == 1) dp[1][i][j] = i + j - 2;
    }
  for (int i = 2; i <= p; i++) {
    memset(dp[i % 2], -1, sizeof(dp[i % 2]));
    for (auto it : G[i - 1]) {
      int x = it.first;
      int y = it.second;
      for (int j = 1; j <= m; j++)
        if (dp[1 - i % 2][x][j] == -1)
          dp[1 - i % 2][x][j] = dp[1 - i % 2][x][y] + abs(y - j);
        else
          dp[1 - i % 2][x][j] =
              min(dp[1 - i % 2][x][j], dp[1 - i % 2][x][y] + abs(y - j));
    }
    for (auto it : G[i]) {
      int x = it.first;
      int y = it.second;
      for (int j = 1; j <= n; j++)
        if (dp[1 - i % 2][j][y] != -1) {
          if (dp[i % 2][x][y] != -1)
            dp[i % 2][x][y] =
                min(dp[i % 2][x][y], dp[1 - i % 2][j][y] + abs(x - j));
          else
            dp[i % 2][x][y] = dp[1 - i % 2][j][y] + abs(x - j);
        }
    }
  }
  cout << dp[p % 2][G[p][0].first][G[p][0].second];
  return 0;
}
