#include <bits/stdc++.h>
using namespace std;
const int N = 305;
const int INF = (int)1e9;
int a[N][N];
int dp[N][N];
vector<pair<int, int>> ps[N * N];
int help[N][N];
int dst(pair<int, int> x, pair<int, int> y) {
  return abs(x.first - y.first) + abs(x.second - y.second);
}
int main() {
  int n, m, p;
  scanf("%d%d%d", &n, &m, &p);
  int fx = 0, fy = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      scanf("%d", &a[i][j]);
      dp[i][j] = (a[i][j] == 1 ? i + j : INF);
      ps[a[i][j]].push_back(make_pair(i, j));
      if (a[i][j] == p) fx = i, fy = j;
    }
  for (int i = 2; i <= p; ++i) {
    if (ps[i - 1].size() < N) {
      for (pair<int, int> cur : ps[i])
        for (pair<int, int> lst : ps[i - 1])
          dp[cur.first][cur.second] =
              min(dp[cur.first][cur.second],
                  dp[lst.first][lst.second] + dst(cur, lst));
    } else {
      for (int x = 0; x < n; ++x) fill(help[x], help[x] + m, INF);
      for (pair<int, int> lst : ps[i - 1])
        for (int y = 0; y < m; ++y)
          help[lst.first][y] =
              min(help[lst.first][y],
                  dp[lst.first][lst.second] + abs(lst.second - y));
      for (pair<int, int> cur : ps[i])
        for (int x = 0; x < n; ++x)
          dp[cur.first][cur.second] =
              min(dp[cur.first][cur.second],
                  help[x][cur.second] + abs(x - cur.first));
    }
  }
  cout << dp[fx][fy];
  return 0;
}
