#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int A[310][310];
int C[310][310];
int d[310][310];
list<pair<int, int> > L[300 * 300 + 100];
int Dist(pair<int, int> a, pair<int, int> b) {
  return abs(a.first - b.first) + abs(b.second - a.second);
}
class comp {
 public:
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
    return C[a.first][a.second] > C[b.first][b.second];
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> p;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> A[i][j];
      L[A[i][j]].push_back(pair<int, int>(i, j));
    }
  for (auto it = L[1].begin(); it != L[1].end(); ++it)
    d[(*it).first][(*it).second] = (*it).second + (*it).first - 2;
  for (int i = 2; i <= p; ++i) {
    if (sqrt(L[i].size()) * sqrt(L[i - 1].size()) < 4 * sqrt(m * n))
      for (auto it = L[i].begin(); it != L[i].end(); ++it) {
        int& Di = d[(*it).first][(*it).second];
        Di = 1000000000;
        {
          for (auto it2 = L[i - 1].begin(); it2 != L[i - 1].end(); ++it2)
            Di = min(Di, d[(*it2).first][(*it2).second] + Dist(*it, *it2));
        }
      }
    else {
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) C[i][j] = -1;
      priority_queue<pair<int, int>, vector<pair<int, int> >, comp> Pq;
      for (auto it = L[i - 1].begin(); it != L[i - 1].end(); ++it) {
        C[(*it).first][(*it).second] = d[(*it).first][(*it).second];
        Pq.push(*it);
      }
      while (!Pq.empty()) {
        pair<int, int> c = Pq.top();
        Pq.pop();
        if (C[c.first + -1][c.second + 0] == -1) {
          C[c.first + -1][c.second + 0] = C[c.first][c.second] + 1;
          Pq.push(pair<int, int>(c.first + -1, c.second + 0));
        };
        if (C[c.first + 1][c.second + 0] == -1) {
          C[c.first + 1][c.second + 0] = C[c.first][c.second] + 1;
          Pq.push(pair<int, int>(c.first + 1, c.second + 0));
        };
        if (C[c.first + 0][c.second + 1] == -1) {
          C[c.first + 0][c.second + 1] = C[c.first][c.second] + 1;
          Pq.push(pair<int, int>(c.first + 0, c.second + 1));
        };
        if (C[c.first + 0][c.second + -1] == -1) {
          C[c.first + 0][c.second + -1] = C[c.first][c.second] + 1;
          Pq.push(pair<int, int>(c.first + 0, c.second + -1));
        };
      }
      for (auto it = L[i].begin(); it != L[i].end(); ++it)
        d[(*it).first][(*it).second] = C[(*it).first][(*it).second];
    }
  }
  int x = L[p].front().first;
  int y = L[p].front().second;
  cout << d[x][y] << endl;
}
