#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, d;
  node() { x = 0, y = 0, d = 0; }
  node(int a, int b, int c) { x = a, y = b, d = c; }
  bool operator<(const node &other) const { return d < other.d; }
};
vector<node> dp[305 * 305];
int n, m, p, x;
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      scanf("%d", &x), dp[x].push_back(node(i, j, (int)1e9));
  dp[0].push_back(node());
  auto dist = [&](node a, node b) -> int {
    return abs(a.x - b.x) + abs(a.y - b.y);
  };
  for (int i = 0; i < p; i++) {
    sort(dp[i].begin(), dp[i].end());
    for (int j = 0; j < ((int)dp[i].size() < 600 ? (int)dp[i].size() : 600);
         j++) {
      for (auto &v : dp[i + 1]) {
        v.d = (v.d < dp[i][j].d + dist(v, dp[i][j])
                   ? v.d
                   : dp[i][j].d + dist(v, dp[i][j]));
      }
    }
  }
  sort(dp[p].begin(), dp[p].end());
  cout << dp[p][0].d << '\n';
  return 0;
}
