#include <bits/stdc++.h>   using namespace std;   const int N = (int)4e2;   const int INF = (int)1e9;   int n, m, a[N][N], p;   int dyn[N][N], last[N];   vector<pair<int, int> > q[N * N];   int len[N][N];   int main() {     cin >> n >> m >> p;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         cin >> a[i][j];         q[a[i][j]].push_back(make_pair(i, j));         dyn[i][j] = INF;       }     }     for (int i = 0; i < m; ++i) {       dyn[0][i] = i;     }     for (int lvl = 1; lvl <= p; ++lvl) {       int x, y;       for (auto tmp : q[lvl]) {         tie(x, y) = tmp;         int ans = INF;         for (int row = 0; row < n; ++row) {           if (last[row] == lvl - 1) {             ans = min(ans, dyn[row][y] + abs(row - x));           }         }         len[x][y] = ans;       }       for (auto tmp : q[lvl]) {         tie(x, y) = tmp;         if (last[x] != lvl) {           last[x] = lvl;           for (int col = 0; col < m; ++col) {             dyn[x][col] = len[x][y] + abs(y - col);           }         } else {           for (int col = 0; col < m; ++col) {             dyn[x][col] = min(dyn[x][col], len[x][y] + abs(y - col));           }         }       }     }     cout << len[q[p][0].first][q[p][0].second] <<   n ;     return 0;   }   