#include <bits/stdc++.h>
using namespace std;
const long long OO = 1e9;
const int MAX = 305;
vector<pair<int, pair<int, int> > > a[MAX * MAX];
int main() {
  int n, m, p;
  cin >> n >> m >> p;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x;
      scanf("%d", &x);
      a[x].push_back(make_pair(OO, make_pair(i, j)));
    }
  a[0].push_back(make_pair(0, make_pair(0, 0)));
  int t = 0;
  for (int key = 1; key <= p; key++) {
    t = !t;
    sort(((a[key - 1]).begin()), ((a[key - 1]).end()));
    for (int i = 0; i < a[key].size(); i++) {
      for (int j = 0; j < min((int)a[key - 1].size(), n + m); j++) {
        int dis = abs(a[key][i].second.first - a[key - 1][j].second.first) +
                  abs(a[key][i].second.second - a[key - 1][j].second.second);
        a[key][i].first = min(a[key - 1][j].first + dis, a[key][i].first);
      }
    }
  }
  cout << a[p][0].first << endl;
}
