#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
int dp[maxn][maxn], mat[maxn][maxn];
int cnt[maxn * maxn], inq[maxn][maxn], d[maxn][maxn];
vector<pair<int, int> > G[maxn * maxn];
int n, m, p;
inline int get_dis(int x1, int y1, int x2, int y2) {
  return abs(x1 - x2) + abs(y1 - y2);
}
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
void init() {
  memset(dp, 0x7f, sizeof(dp));
  memset(cnt, 0, sizeof(cnt));
}
int main() {
  while (scanf("%d%d%d", &n, &m, &p) == 3 && n) {
    init();
    int xt, yt;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", &mat[i][j]);
        if (mat[i][j] == 1) dp[i][j] = get_dis(0, 0, i, j);
        if (mat[i][j] == p) xt = i, yt = j;
        cnt[mat[i][j]]++;
        G[mat[i][j]].push_back(make_pair(i, j));
      }
    }
    for (int i = 2; i <= p; i++) {
      if (cnt[i - 1] * cnt[i] <= m * n) {
        for (int j = 0; j < G[i].size(); j++) {
          int x2 = G[i][j].first, y2 = G[i][j].second;
          for (int k = 0; k < G[i - 1].size(); k++) {
            int x1 = G[i - 1][k].first, y1 = G[i - 1][k].second;
            dp[x2][y2] = min(dp[x2][y2], dp[x1][y1] + get_dis(x1, y1, x2, y2));
          }
        }
      } else {
        memset(d, 0x7f, sizeof(d));
        memset(inq, 0, sizeof(inq));
        queue<pair<int, int> > Q;
        for (int j = 0; j < G[i - 1].size(); j++) {
          pair<int, int> u = G[i - 1][j];
          d[u.first][u.second] = dp[u.first][u.second],
          inq[u.first][u.second] = 1;
          Q.push(make_pair(u.first, u.second));
        }
        while (!Q.empty()) {
          pair<int, int> u = Q.front();
          Q.pop();
          inq[u.first][u.second] = 0;
          if (mat[u.first][u.second] == i)
            dp[u.first][u.second] =
                min(dp[u.first][u.second], d[u.first][u.second]);
          for (int j = 0; j < 4; j++) {
            int x = u.first + dx[j], y = u.second + dy[j];
            if (x < 0 || x >= n || y < 0 || y >= m) continue;
            if (d[x][y] > d[u.first][u.second] + 1) {
              d[x][y] = d[u.first][u.second] + 1;
              if (!inq[x][y]) inq[x][y] = 1, Q.push(make_pair(x, y));
            }
          }
        }
      }
    }
    printf("%d\n", dp[xt][yt]);
  }
  return 0;
}
