#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, long long> > place[90004];
vector<pair<int, long long> > temp[305];
vector<long long> MIN[2][305];
int bin(int i, int j, const int &b, const int &x) {
  if (i == j) return i;
  if (i == j - 1) {
    if (temp[x][i].first >= b) return i;
    return j;
  }
  int mid = (i + j) >> 1;
  if (temp[x][mid].first >= b) return bin(i, mid, b, x);
  return bin(mid + 1, j, b, x);
}
long long rq(int x, int b) {
  int idx = bin(0, temp[x].size() - 1, b, x);
  if (!idx)
    return MIN[1][x][idx] - b;
  else
    return min(MIN[1][x][idx] - b, b + MIN[0][x][idx - 1]);
}
int main() {
  int n, m, p;
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &p);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      scanf("%d", &x);
      if (x == 1)
        place[x].push_back(make_pair(make_pair(i, j), abs(i - 1) + abs(j - 1)));
      else
        place[x].push_back(make_pair(make_pair(i, j), 0));
    }
  }
  for (int i = 1; i < p; i++) {
    for (int j = 1; j <= n; j++) temp[j].clear();
    for (int j = 0; j < place[i].size(); j++)
      temp[place[i][j].first.first].push_back(
          make_pair(place[i][j].first.second, place[i][j].second));
    for (int x = 1; x <= n; x++) {
      temp[x].push_back(make_pair(302, 1e18));
      sort(temp[x].begin(), temp[x].end());
      MIN[0][x].resize(temp[x].size());
      MIN[1][x].resize(temp[x].size());
      MIN[0][x][0] = temp[x][0].second - temp[x][0].first;
      for (int i = 1; i < temp[x].size(); i++)
        MIN[0][x][i] =
            min(MIN[0][x][i - 1], temp[x][i].second - temp[x][i].first);
      int last = temp[x].size() - 1;
      MIN[1][x][last] = temp[x][last].first + temp[x][last].second;
      for (int i = last - 1; i >= 0; i--)
        MIN[1][x][i] =
            min(MIN[1][x][i + 1], temp[x][i].first + temp[x][i].second);
    }
    for (int j = 0; j < place[i + 1].size(); j++) {
      long long mn = 1000000007 * 1LL * 1000000007;
      for (int x = 1; x <= n; x++) {
        long long ans = abs(x - place[i + 1][j].first.first);
        ans += rq(x, place[i + 1][j].first.second);
        if (ans < mn) mn = ans;
      }
      place[i + 1][j].second = mn;
    }
  }
  long long ans = 1000000007 * 1LL * 1000000007;
  for (int i = 0; i < place[p].size(); i++) ans = min(ans, place[p][i].second);
  printf("%lld\n", ans);
}
