#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MAX = 305;
int dp[MAX][MAX];
int d[MAX][MAX];
vector<pair<int, int> > sq[MAX * MAX];
int n, m, p;
int dist(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
int ok(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool comp(pair<int, int> a, pair<int, int> b) {
  return dp[a.first][a.second] < dp[b.first][b.second];
}
int main() {
  scanf("%d %d %d", &n, &m, &p);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int k;
      scanf("%d", &k);
      sq[k].push_back(make_pair(i, j));
    }
  }
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = MOD;
    }
  }
  for (pair<int, int> i : sq[1]) {
    dp[i.first][i.second] = i.first + i.second;
  }
  for (int i = 2; i < p + 1; i++) {
    if (sq[i].size() <= n) {
      for (pair<int, int> a : sq[i - 1]) {
        for (pair<int, int> b : sq[i]) {
          dp[b.first][b.second] =
              min(dp[b.first][b.second], dp[a.first][a.second] + dist(a, b));
        }
      }
    } else {
      for (int a = 0; a < MAX; a++) {
        for (int b = 0; b < MAX; b++) {
          d[a][b] = MOD;
        }
      }
      queue<pair<int, pair<int, int> > > q;
      queue<pair<int, int> > q2;
      vector<pair<int, int> > v = sq[i - 1];
      sort(v.begin(), v.end(), comp);
      for (pair<int, int> i : v) {
        q2.push(i);
      }
      q.push(make_pair(dp[v[0].first][v[0].second], v[0]));
      while (!q.empty()) {
        auto t = q.front();
        if (!q2.empty()) {
          pair<int, int> orig = q2.front();
          if (dp[orig.first][orig.second] <= t.first) {
            t = make_pair(dp[orig.first][orig.second], orig);
            q2.pop();
          } else {
            q.pop();
          }
        } else {
          q.pop();
        }
        if (d[t.second.first][t.second.second] > t.first &&
            d[t.second.first][t.second.second] != MOD) {
          continue;
        }
        d[t.second.first][t.second.second] = t.first;
        for (int a = 0; a < 4; a++) {
          int nx = t.second.first + dx[a];
          int ny = t.second.second + dy[a];
          if (ok(nx, ny) && d[nx][ny] == MOD) {
            d[nx][ny] = min(d[nx][ny], d[t.second.first][t.second.second] + 1);
            q.push(make_pair(d[nx][ny], make_pair(nx, ny)));
          }
        }
      }
      for (pair<int, int> b : sq[i]) {
        dp[b.first][b.second] = d[b.first][b.second];
      }
    }
  }
  int ans = MOD;
  for (pair<int, int> i : sq[p]) {
    ans = min(ans, dp[i.first][i.second]);
  }
  printf("%d", ans);
}
