#include <bits/stdc++.h>   using namespace std;   const int MAXN = 305;   struct sss {     int a, b, c;     bool operator<(const sss& a) const { return c < a.c; }   };   vector<sss> v[MAXN * MAXN];   int cost(int a, int b, int c, int d) { return abs(a - c) + abs(b - d); }   int main() {     int m, n, i, j, k, p;     cin >> m >> n >> p;     for (i = 1; i <= m; i++) {       for (j = 1; j <= n; j++) {         int num;         cin >> num;         v[num].push_back({i, j, 999999999});       }     }     i = 1;     for (j = 0; j < v[i].size(); j++) {       v[i][j].c = cost(v[i][j].a, v[i][j].b, 1, 1);     }     for (i = 2; i <= p; i++) {       sort((v[i - 1]).begin(), (v[i - 1]).end());       for (j = 0; j < v[i].size(); j++) {         for (k = 0; k < min((int)v[i - 1].size(), m + n + 2); k++) {           v[i][j].c =               min(v[i][j].c, v[i - 1][k].c + cost(v[i - 1][k].a, v[i - 1][k].b,                                                   v[i][j].a, v[i][j].b));         }       }     }     int ans = 999999999;     i = p;     for (j = 0; j < v[i].size(); j++) ans = min(ans, v[i][j].c);     cout << ans << endl;   }   