#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int a[312][312];
vector<pair<int, pair<int, int> > > tab[312 * 312];
int d[312][312];
int vis[312][312];
int x[4] = {0, 0, 1, -1};
int y[4] = {1, -1, 0, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> p;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  int start = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      tab[a[i][j]].push_back(make_pair(0x3f3f3f3f, pair<int, int>(i, j)));
  for (int i = 0; i < tab[start].size(); ++i) {
    tab[start][i].first =
        tab[start][i].second.first + tab[start][i].second.second;
  }
  for (int i = start + 1; i <= p; ++i) {
    if (tab[i].size() * tab[i - 1].size() >= n * m) {
      for (int t1 = 0; t1 < n; ++t1)
        for (int t2 = 0; t2 < m; ++t2) d[t1][t2] = 0x3f3f3f3f, vis[t1][t2] = 0;
      queue<pair<int, pair<int, int> > > Q;
      sort(tab[i - 1].begin(), tab[i - 1].end());
      int j = 0;
      Q.push(tab[i - 1][j++]);
      d[tab[i - 1][0].second.first][tab[i - 1][0].second.second] =
          tab[i - 1][0].first;
      while (!Q.empty()) {
        pair<int, pair<int, int> > p = Q.front();
        Q.pop();
        int e = p.second.first;
        int f = p.second.second;
        vis[e][f] = 1;
        int g = p.first;
        while (j < tab[i - 1].size() && g == tab[i - 1][j].first) {
          if (!vis[tab[i - 1][j].second.first][tab[i - 1][j].second.second]) {
            vis[tab[i - 1][j].second.first][tab[i - 1][j].second.second] = 1;
            d[tab[i - 1][j].second.first][tab[i - 1][j].second.second] =
                tab[i - 1][j].first;
            Q.push(tab[i - 1][j]);
          }
          j++;
        }
        for (int k = 0; k < 4; ++k) {
          int d1 = e + x[k];
          int d2 = f + y[k];
          if (d1 >= 0 && d1 < n && d2 >= 0 && d2 < m && !vis[d1][d2]) {
            d[d1][d2] = d[e][f] + 1;
            vis[d1][d2] = 1;
            Q.push(make_pair(d[d1][d2], pair<int, int>(d1, d2)));
          }
        }
      }
      for (int k = 0; k < tab[i].size(); ++k) {
        tab[i][k].first = d[tab[i][k].second.first][tab[i][k].second.second];
      }
      continue;
    }
    for (int j = 0; j < tab[i].size(); ++j) {
      for (int k = 0; k < tab[i - 1].size(); ++k) {
        int d1 = abs(tab[i][j].second.first - tab[i - 1][k].second.first);
        int d2 = abs(tab[i][j].second.second - tab[i - 1][k].second.second);
        tab[i][j].first = min(tab[i][j].first, tab[i - 1][k].first + d1 + d2);
      }
    }
  }
  cout << tab[p][0].first << endl;
  return 0;
}
