#include <bits/stdc++.h>
using namespace std;
void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
template <typename... T>
void shit(T&... x) {}
template <typename... T>
void dec(T&... x) {
  shit(--x...);
}
template <typename... T>
void inc(T&... x) {
  shit(++x...);
}
template <typename T>
string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}
template <typename T>
T dcm(string& second) {
  T x = 0;
  for (int i = 0; i < second.size(); i++) {
    x = (x * 2) + (second[i] == '1');
  }
  return x;
}
template <typename T>
T input() {
  T ans = 0, m = 1;
  char c = ' ';
  while (c == ' ' || c == '\n') c = getchar();
  if (c == '-') m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + int(c - '0'), c = getchar();
  }
  return ans * m;
}
const int inf = 1e9;
const double eps = 1e-9;
const int maxn = 3e2 + 10, base = 1e9 + 7;
const int sigm = 26;
const long long llinf = 1e18;
template <typename T>
T binpow(T n, T second) {
  if (second <= 0) return 1LL;
  if (second % 2 == 0) {
    T b = binpow(n, second / 2);
    return (1LL * b * b) % base;
  } else {
    return (1LL * binpow(n, second - 1) * n) % base;
  }
}
vector<pair<int, int> > thisColor[maxn * maxn];
int dp[maxn][maxn], dp2[maxn][maxn];
int a[maxn][maxn];
int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }
int getDp(const pair<int, int>& kek) { return dp[kek.first][kek.second]; }
int getDp2(const pair<int, int>& kek) { return dp2[kek.first][kek.second]; }
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
  return getDp(a) < getDp(b);
}
int px[4] = {1, 0, -1, 0};
int py[4] = {0, 1, 0, -1};
int main() {
  memset(dp, 63, sizeof(dp));
  int n, m, p;
  scanf("%d %d %d", &n, &m, &p);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      if (a[i][j] == 1) dp[i][j] = i + j;
      thisColor[a[i][j]].push_back(make_pair(i, j));
    }
  int block = (n + m) / 2;
  for (int color = 2; color <= p; color++) {
    if (thisColor[color].size() >= block ||
        thisColor[color - 1].size() >= block) {
      deque<pair<int, int> > q, waitingShit;
      vector<pair<int, int> > tmp;
      for (int i = 0; i < thisColor[color - 1].size(); i++)
        tmp.push_back(thisColor[color - 1][i]);
      sort(tmp.begin(), tmp.end(), cmp);
      memset(dp2, 63, sizeof(dp2));
      for (int i = 0; i < tmp.size(); i++) waitingShit.push_back(tmp[i]);
      while (!(q.empty() && waitingShit.empty())) {
        while (q.empty() || ((waitingShit.size()) &&
                             getDp(waitingShit.front()) <= getDp2(q.back()))) {
          if (q.empty() && waitingShit.empty()) break;
          pair<int, int> newComer = waitingShit.front();
          if (dp2[newComer.first][newComer.second] > getDp(newComer)) {
            q.push_back(newComer);
            dp2[newComer.first][newComer.second] = getDp(newComer);
          }
          waitingShit.pop_front();
        }
        if (q.empty() && waitingShit.empty()) break;
        int x = q.front().first, y = q.front().second;
        q.pop_front();
        for (int i = 0; i < 4; i++) {
          int nx = x + px[i], ny = y + py[i];
          if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
              dp2[nx][ny] > dp2[x][y] + 1) {
            q.push_back(make_pair(nx, ny));
            dp2[nx][ny] = dp2[x][y] + 1;
          }
        }
      }
      for (int i = 0; i < thisColor[color].size(); i++) {
        int x = thisColor[color][i].first, y = thisColor[color][i].second;
        dp[x][y] = dp2[x][y];
      }
    } else {
      for (int i = 0; i < thisColor[color].size(); i++)
        for (int j = 0; j < thisColor[color - 1].size(); j++) {
          int x1 = thisColor[color][i].first, y1 = thisColor[color][i].second;
          int x2 = thisColor[color - 1][j].first,
              y2 = thisColor[color - 1][j].second;
          dp[x1][y1] = min(dp[x1][y1], dp[x2][y2] + dist(x1, y1, x2, y2));
        }
    }
  }
  int ans = inf;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == p) {
        ans = min(ans, dp[i][j]);
      }
  cout << ans;
  return 0;
}
