#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 6, inf = 1e18, mod = 1e9 + 7;
long long n, m, p, dis[305][305], a[305][305], tmp[305][305];
vector<pair<int, int> > vec[305 * 305], V;
bool mark[305][305];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> p;
  if (p == 1) return cout << 0 << endl, 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> a[i][j];
      vec[a[i][j]].push_back({i, j});
      if (a[i][j] == 1) {
        dis[i][j] = i + j;
        mark[i][j] = 1;
        V.push_back({i, j});
      } else
        dis[i][j] = inf;
    }
  }
  long long x = 1;
  while (x < p) {
    for (auto i : vec[x]) tmp[i.first][i.second] = dis[i.first][i.second];
    for (auto i : V) {
      dis[i.first][i.second] = inf;
      mark[i.first][i.second] = 0;
    }
    V.clear();
    for (auto i : vec[x]) {
      dis[i.first][i.second] = tmp[i.first][i.second];
      for (long long j = 0; j < m; j++) {
        dis[i.first][j] =
            min(dis[i.first][j], dis[i.first][i.second] + abs(i.second - j));
        if (!mark[i.first][j]) {
          V.push_back({i.first, j});
          mark[i.first][j] = 1;
        }
      }
      for (long long j = 0; j < n; j++) {
        dis[j][i.second] =
            min(dis[j][i.second], dis[i.first][i.second] + abs(j - i.first));
        if (!mark[j][i.second]) {
          V.push_back({j, i.second});
          mark[j][i.second] = 1;
        }
      }
    }
    for (auto i : vec[x + 1]) {
      for (long long j = 0; j < m; j++)
        dis[i.first][i.second] =
            min(dis[i.first][i.second], dis[i.first][j] + abs(j - i.second));
      for (long long j = 0; j < n; j++)
        dis[i.first][i.second] =
            min(dis[i.first][i.second], dis[j][i.second] + abs(i.first - j));
      if (!mark[i.first][i.second]) {
        V.push_back({i.first, i.second});
        mark[i.first][i.second] = 1;
      }
    }
    x++;
  }
  cout << dis[vec[p][0].first][vec[p][0].second] << endl;
}
