#include <bits/stdc++.h>
using namespace std;
const int N = 3e2;
const long long INF = 4e18;
int n, m, p;
long long matrix[N][N], ans[N][N], bfs[N][N];
bool used[N][N];
struct Coord {
  int x, y;
  Coord() {}
  Coord(long long x, long long y) {
    this->x = x;
    this->y = y;
  }
} bg(0, 0);
vector<pair<int, int> > delta{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<Coord> g[N * N];
long long dist(const Coord& a, const Coord& b) {
  return (abs(a.x - b.x) + abs(a.y - b.y));
}
bool check(int x, int y) { return !(x < 0 || y < 0 || x >= n || y >= m); }
queue<Coord> q;
void dobfs(int color) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      used[i][j] = false;
      if (matrix[i][j] != color)
        bfs[i][j] = INF;
      else {
        bfs[i][j] = ans[i][j];
      }
    }
  Coord cur;
  int nx, ny;
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    for (auto d : delta) {
      nx = cur.x + d.first;
      ny = cur.y + d.second;
      if (check(nx, ny) && bfs[nx][ny] > bfs[cur.x][cur.y] + 1) {
        bfs[nx][ny] = min(bfs[nx][ny], bfs[cur.x][cur.y] + 1);
        q.push(Coord(nx, ny));
      }
    }
  }
}
void solve() {
  cin >> n >> m >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> matrix[i][j];
      g[matrix[i][j]].push_back(Coord(i, j));
      if (matrix[i][j] == 1)
        ans[i][j] = i + j;
      else
        ans[i][j] = INF;
    }
  }
  int pcolor, nx, ny;
  for (int color = 1; color <= p; color++) {
    if (g[color].size() * g[color - 1].size() < n * m) {
      for (auto j : g[color]) {
        for (auto i : g[color - 1]) {
          ans[j.x][j.y] = min(ans[j.x][j.y], ans[i.x][i.y] + dist(i, j));
        }
      }
    } else {
      for (auto j : g[color - 1]) q.push(j);
      dobfs(color - 1);
      for (auto j : g[color]) ans[j.x][j.y] = bfs[j.x][j.y];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (matrix[i][j] == p) {
        cout << ans[i][j];
        return;
      }
  }
}
int main() {
  solve();
  return 0;
}
