#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int table[301][301];
int dist[301][301];
bool mark[301][301];
vector<vector<pair<int, int> > > vvpii;
int CalcDist(pair<int, int> u, pair<int, int> v) {
  return fabs((u.first) - (v.first)) + fabs((u.second) - (v.second));
}
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void bfs(vector<pair<int, int> >& from, vector<pair<int, int> >& to,
         int target) {
  vector<pair<int, pair<int, int> > > fromW(from.size());
  for (int i = 0; i < from.size(); i++) {
    fromW[i].first = dist[from[i].first][from[i].second];
    fromW[i].second.first = from[i].first;
    fromW[i].second.second = from[i].second;
  }
  sort(fromW.begin(), fromW.end());
  memset(mark, false, sizeof(mark));
  int fromInd = 0;
  queue<pair<int, pair<int, int> > > Q;
  Q.push(fromW[fromInd]);
  mark[fromW[fromInd].second.first][fromW[fromInd].second.second] = true;
  fromInd++;
  while (!Q.empty()) {
    pair<int, pair<int, int> > f = Q.front();
    Q.pop();
    while (fromInd < fromW.size() &&
           mark[fromW[fromInd].second.first][fromW[fromInd].second.second]) {
      fromInd++;
    }
    while (fromInd < fromW.size() && fromW[fromInd].first <= f.first) {
      Q.push(fromW[fromInd]);
      fromInd++;
    }
    for (int i = 0; i < 4; i++) {
      int newI = f.second.first + dir[i][0];
      int newJ = f.second.second + dir[i][1];
      if (0 <= newI && newI < n && 0 <= newJ && newJ < m &&
          mark[newI][newJ] != true) {
        Q.push(pair<int, pair<int, int> >(f.first + 1,
                                          pair<int, int>(newI, newJ)));
        mark[newI][newJ] = true;
        if (table[newI][newJ] == target) dist[newI][newJ] = f.first + 1;
      }
    }
  }
}
int main() {
  pair<int, int> target;
  cin >> n >> m >> p;
  vvpii = vector<vector<pair<int, int> > >(p + 1, vector<pair<int, int> >());
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < m; j++) {
      cin >> table[i][j];
      if (table[i][j] == 1)
        dist[i][j] = i + j;
      else
        dist[i][j] = -1;
      vvpii[table[i][j]].push_back(pair<int, int>(i, j));
      if (table[i][j] == p) target = pair<int, int>(i, j);
    }
  }
  for (int i = 2; i <= p; i++) {
    if (vvpii[i].size() * vvpii[i - 1].size() < n * m) {
      for (int j = 0; j < vvpii[i].size(); j++) {
        dist[vvpii[i][j].first][vvpii[i][j].second] =
            CalcDist(vvpii[i - 1][0], vvpii[i][j]) +
            dist[vvpii[i - 1][0].first][vvpii[i - 1][0].second];
        for (int k = 1; k < vvpii[i - 1].size(); k++) {
          if (CalcDist(vvpii[i - 1][k], vvpii[i][j]) +
                  dist[vvpii[i - 1][k].first][vvpii[i - 1][k].second] <
              dist[vvpii[i][j].first][vvpii[i][j].second])
            dist[vvpii[i][j].first][vvpii[i][j].second] =
                CalcDist(vvpii[i - 1][k], vvpii[i][j]) +
                dist[vvpii[i - 1][k].first][vvpii[i - 1][k].second];
        }
      }
    } else {
      bfs(vvpii[i - 1], vvpii[i], i);
    }
  }
  cout << dist[target.first][target.second] << endl;
  return 0;
}
