#include <bits/stdc++.h>
using namespace std;
using Point = pair<int, int>;
template <typename T>
using Matrix = vector<vector<T>>;
const array<Point, 4> directions = {{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}};
void bfs(int R, int C, const vector<Point>& source, const vector<Point>& target,
         Matrix<int>& global_distance) {
  vector<pair<int, Point>> sorted;
  for (const auto& p : source) {
    int r, c;
    tie(r, c) = p;
    sorted.push_back({global_distance[r][c], p});
  }
  sort(begin(sorted), end(sorted));
  Matrix<int> distance(R, vector<int>(C, INT_MAX));
  deque<pair<int, Point>> q = {sorted[0]};
  int i = 1;
  while (!q.empty()) {
    int d;
    Point p;
    tie(d, p) = q.front();
    q.pop_front();
    int r, c;
    tie(r, c) = p;
    if (distance[r][c] <= d)
      continue;
    else
      distance[r][c] = d;
    for (; i < sorted.size(); i++) {
      int nd;
      Point np;
      tie(nd, np) = sorted[i];
      if (nd != d) break;
      q.push_front({nd, np});
    }
    for (const auto& dir : directions) {
      int dr, dc;
      tie(dr, dc) = dir;
      int nr = r + dr, nc = c + dc;
      if (!(0 <= nr && nr < R && 0 <= nc && nc < C)) continue;
      q.push_back({d + 1, {nr, nc}});
    }
  }
  for (const auto& p : target) {
    int r, c;
    tie(r, c) = p;
    global_distance[r][c] = distance[r][c];
  }
}
void dag(const vector<Point>& source, const vector<Point>& target,
         Matrix<int>& distance) {
  for (const auto& p : source)
    for (const auto& q : target) {
      int r, c;
      tie(r, c) = p;
      int nr, nc;
      tie(nr, nc) = q;
      distance[nr][nc] =
          min(distance[nr][nc], distance[r][c] + abs(nr - r) + abs(nc - c));
    }
}
int solve(int R, int C, int P, Matrix<int> A) {
  vector<vector<Point>> rank(P);
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++) rank[A[r][c]].push_back({r, c});
  Matrix<int> distance(R, vector<int>(C, INT_MAX));
  for (const auto& p : rank[0]) {
    int r, c;
    tie(r, c) = p;
    distance[r][c] = r + c;
  }
  for (int level = 0; level + 1 < P; level++) {
    int a = rank[level].size();
    int b = rank[level + 1].size();
    if (a * a > R * C || b * b > R * C)
      bfs(R, C, rank[level], rank[level + 1], distance);
    else
      dag(rank[level], rank[level + 1], distance);
  }
  int r, c;
  tie(r, c) = rank[P - 1][0];
  return distance[r][c];
}
int main() {
  int R, C, P;
  cin >> R >> C >> P;
  Matrix<int> A(R, vector<int>(C));
  for (int r = 0; r < R; r++)
    for (int c = 0; c < C; c++) {
      cin >> A[r][c];
      A[r][c]--;
    }
  cout << solve(R, C, P, move(A)) << endl;
  vector<int> v;
  return 0;
}
