#include <bits/stdc++.h>
#pragma GCC optimize(2)
int n, m, k, mp[305][305];
std::vector<std::pair<int, int> > garden[90005];
int dis[305][305], dis2[305][305];
bool vis[305][305];
int dist(std::pair<int, int> a, std::pair<int, int> b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
void refresh(int lev) {
  for (int i = 0; i < garden[lev].size(); ++i) {
    int ux = garden[lev][i].first, uy = garden[lev][i].second;
    for (int j = 0; j < garden[lev + 1].size(); ++j) {
      int vx = garden[lev + 1][j].first, vy = garden[lev + 1][j].second;
      dis[vx][vy] = std::min(
          dis[vx][vy], dis[ux][uy] + dist(garden[lev][i], garden[lev + 1][j]));
    }
  }
}
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void bfs(int lev) {
  memset(dis2, 0x3f, sizeof(dis2));
  static std::queue<int> qx, qy;
  for (int i = 0; i < garden[lev].size(); ++i) {
    int ux = garden[lev][i].first, uy = garden[lev][i].second;
    dis2[ux][uy] = dis[ux][uy];
    qx.push(ux);
    qy.push(uy);
    vis[ux][uy] = 1;
  }
  while (!qx.empty()) {
    int ux = qx.front(), uy = qy.front();
    qx.pop();
    qy.pop();
    vis[ux][uy] = 0;
    for (int i = 0; i < 4; ++i) {
      int vx = ux + dx[i], vy = uy + dy[i];
      if (vx < 0 || vx >= n || vy < 0 || vy >= m) continue;
      if (dis2[vx][vy] > dis2[ux][uy] + 1) {
        dis2[vx][vy] = dis2[ux][uy] + 1;
        if (!vis[vx][vy]) {
          qx.push(vx);
          qy.push(vy);
          vis[vx][vy] = 1;
        }
      }
    }
  }
  for (int i = 0; i < garden[lev + 1].size(); ++i) {
    int ux = garden[lev + 1][i].first, uy = garden[lev + 1][i].second;
    dis[ux][uy] = dis2[ux][uy];
  }
}
void calc() {
  for (int i = 0; i < garden[1].size(); ++i) {
    int ux = garden[1][i].first, uy = garden[1][i].second;
    dis[ux][uy] = dist(garden[1][i], std::pair<int, int>(0, 0));
  }
  for (int lev = 1; lev < k; ++lev) {
    if (garden[lev].size() * garden[lev + 1].size() < n * m) {
      refresh(lev);
    } else {
      bfs(lev);
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &mp[i][j]);
      garden[mp[i][j]].push_back(std::pair<int, int>(i, j));
    }
  }
  memset(dis, 0x3f, sizeof(dis));
  calc();
  int res = 0x3f3f3f3f;
  for (int i = 0; i < garden[k].size(); ++i) {
    int ux = garden[k][i].first, uy = garden[k][i].second;
    res = std::min(res, dis[ux][uy]);
  }
  printf("%d", res);
}
