#include <bits/stdc++.h>
using namespace std;
const int N = 600005;
const long long mod = (long long)1e9 + 7;
int a[305][305], n, m;
int dist[305][305], val[100000], tmpdist[305][305];
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
vector<vector<pair<int, int> > > l;
int diff(pair<int, int> u, pair<int, int> v) {
  return abs(u.first - v.first) + abs(u.second - v.second);
}
bool bound(int i, int j) {
  if (i >= 0 && i < n && j >= 0 && j < m) return 1;
  return 0;
}
void bfs(int color) {
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >,
                 greater<pair<int, pair<int, int> > > >
      pq;
  memset(tmpdist, -1, sizeof tmpdist);
  for (int i = 0, t = l[color - 1].size(); i < t; i++) {
    pair<int, int> u = l[color - 1][i];
    pq.push(make_pair(dist[u.first][u.second], u));
    tmpdist[u.first][u.second] = dist[u.first][u.second];
  }
  while (!pq.empty()) {
    pair<int, pair<int, int> > p = pq.top();
    pq.pop();
    int d = p.first;
    pair<int, int> u = p.second;
    if (d > tmpdist[u.first][u.second]) continue;
    for (int k = 0, t = 4; k < t; k++) {
      int ui = u.first + di[k], uj = u.second + dj[k];
      if (bound(ui, uj) && (tmpdist[ui][uj] == -1 || tmpdist[ui][uj] > d + 1)) {
        tmpdist[ui][uj] = d + 1;
        pq.push(make_pair(d + 1, pair<int, int>(ui, uj)));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int p;
  cin >> n >> m >> p;
  l.resize(p + 1);
  for (int i = 0, t = n; i < t; i++) {
    for (int j = 0, t = m; j < t; j++) {
      cin >> a[i][j];
      int v = i * m + j;
      val[v] = a[i][j];
      l[a[i][j]].push_back(pair<int, int>(i, j));
      if (a[i][j] == 1)
        dist[i][j] = i + j;
      else
        dist[i][j] = 1e9;
    }
  }
  for (int i = 2, t = p + 1; i < t; i++) {
    if (l[i].size() * l[i - 1].size() < 10 * n * m) {
      for (int j = 0, t = l[i].size(); j < t; j++) {
        int mn = 1e9;
        pair<int, int> u = l[i][j];
        for (int k = 0, t = l[i - 1].size(); k < t; k++) {
          pair<int, int> v = l[i - 1][k];
          mn = min(mn, dist[v.first][v.second] + diff(u, v));
        }
        dist[u.first][u.second] = mn;
      }
    } else {
      bfs(i);
      for (int j = 0, t = l[i].size(); j < t; j++) {
        pair<int, int> u = l[i][j];
        dist[u.first][u.second] = tmpdist[u.first][u.second];
      }
    }
  }
  cout << dist[l[p][0].first][l[p][0].second] << '\n';
}
