#include <bits/stdc++.h>
using namespace std;
struct node {
  int dis, pos;
} b[3050000], now;
int a[310][310];
int dp[310][310];
bool cmp(node a, node b) { return a.dis < b.dis; }
vector<node> mp[310 * 310];
int main() {
  int n, m, p;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) dp[i][j] = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      if (a[i][j] == 1) {
        dp[i][j] = i + j - 2;
        now.dis = dp[i][j];
      }
      now.pos = (i - 1) * m + j;
      mp[a[i][j]].push_back(now);
    }
  }
  sort(mp[1].begin(), mp[1].end(), cmp);
  for (int i = 2; i <= p; i++) {
    int sz1 = mp[i].size();
    int sz2 = mp[i - 1].size();
    for (int j = 0; j < sz1; j++) {
      int x = mp[i][j].pos / m + 1;
      int y = mp[i][j].pos % m;
      if (y == 0) y = m, x--;
      for (int k = 0; k < sz2 && k < 1000; k++) {
        int xx = mp[i - 1][k].pos / m + 1;
        int yy = mp[i - 1][k].pos % m;
        if (yy == 0) yy = m, xx--;
        dp[x][y] = min(dp[x][y], dp[xx][yy] + abs(x - xx) + abs(y - yy));
      }
      mp[i][j].dis = dp[x][y];
    }
    sort(mp[i].begin(), mp[i].end(), cmp);
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == p) ans = min(dp[i][j], ans);
    }
  printf("%d\n", ans);
  return 0;
}
