#include <bits/stdc++.h>   using namespace std;   const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},                          {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   template <typename T, typename TT>   ostream& operator<<(ostream& s, pair<T, TT> t) {     return s <<  (  << t.first <<  ,  << t.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& s, vector<T> t) {     s <<  [ ;     for (int i = 0; i < ((int)(t.size())) - 1; i++) s << t[i] <<  ,  ;     s << t[((int)(t.size())) - 1] <<  ] ;     return s;   }   const int MOD = 1e9 + 7;   template <typename T>   inline T gcD(T a, T b) {     if (a < b) swap(a, b);     while (b) {       a = a % b;       b ^= a;       a ^= b;       b ^= a;     };     return a;   }   template <typename T>   inline T pow_mod(T a, T b) {     T res = 1;     while (b) {       if (b & 1) res = (res * a) % MOD;       a = (a * a) % MOD;       b >>= 1;     }     return res;   }   struct Node {     int value, x, y;     bool operator<(const Node other) const {       if (value == other.value && x == other.x)         return y < other.y;       else if (value == other.value)         return x < other.x;       else         return value < other.value;     }   };   const int MAXN = 310;   int room[MAXN][MAXN];   int dis[MAXN][MAXN];   vector<Node> dp[MAXN * MAXN];   vector<Node> bfs;   int main() {     int n, m, p;     cin >> n >> m >> p;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> room[i][j];         if (room[i][j] == 1) {           dp[1].push_back({i + j, i, j});         } else           dp[room[i][j]].push_back({(int)1e9, i, j});       }     for (int i = 2; i < p + 1; i++) {       if (dp[i - 1].size() * dp[i].size() >= n * m) {         bfs.clear();         sort(dp[i - 1].begin(), dp[i - 1].end());         bfs.push_back(dp[i - 1][0]);         for (int i = 0; i < n; i++)           for (int j = 0; j < m; j++) dis[i][j] = (int)1e9;         dis[dp[i - 1][0].x][dp[i - 1][0].y] = dp[i - 1][0].value;         int pt = 1;         int j = 0;         while (j < bfs.size()) {           Node top = bfs[j++];           while (pt < dp[i - 1].size() && dp[i - 1][pt].value <= top.value)             bfs.push_back(dp[i - 1][pt++]);           for (int k = 0; k < 4; k++) {             int new_x = top.x + fx[k][0];             int new_y = top.y + fx[k][1];             if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < m) {               if (dis[new_x][new_y] > top.value + 1) {                 dis[new_x][new_y] = top.value + 1;                 bfs.push_back({dis[new_x][new_y], new_x, new_y});               }             }           }         }         for (__typeof((dp[i]).begin()) v = (dp[i]).begin(); v != (dp[i]).end();              ++v) {           v->value = dis[v->x][v->y];         }       } else {         for (__typeof((dp[i]).begin()) v = (dp[i]).begin(); v != (dp[i]).end();              ++v) {           for (__typeof((dp[i - 1]).begin()) w = (dp[i - 1]).begin();                w != (dp[i - 1]).end(); ++w) {             v->value =                 min(v->value, w->value + abs(w->x - v->x) + abs(w->y - v->y));           }         }       }     }     cout << dp[p][0].value << endl;     return 0;   }