#include <bits/stdc++.h>
using namespace std;
int N, M, P;
struct _2D_Segment_Tree {
  struct _1D_Segment_Tree {
    int seg[310 * 4];
    void modify(int l, int r, int y, int val, int idx) {
      if (l == r) {
        seg[idx] = val;
      } else {
        int mid = (l + r) >> 1;
        if (y <= mid)
          modify(l, mid, y, val, idx << 1);
        else
          modify(mid + 1, r, y, val, idx << 1 | 1);
        seg[idx] = min(seg[idx << 1], seg[idx << 1 | 1]);
      }
    }
    int query(int l, int r, int yl, int yr, int idx) {
      if (l == yl && r == yr) return seg[idx];
      int mid = (l + r) >> 1;
      if (yr <= mid)
        return query(l, mid, yl, yr, idx << 1);
      else if (yl > mid)
        return query(mid + 1, r, yl, yr, idx << 1 | 1);
      return min(query(l, mid, yl, mid, idx << 1),
                 query(mid + 1, r, mid + 1, yr, idx << 1 | 1));
    }
  } tree[310 * 4];
  void pushUp(int l, int r, int id, int idx, int pos) {
    if (l == r) {
      tree[id].seg[idx] =
          min(tree[id << 1].seg[idx], tree[id << 1 | 1].seg[idx]);
    } else {
      int mid = (l + r) >> 1;
      if (pos <= mid)
        pushUp(l, mid, id, idx << 1, pos);
      else
        pushUp(mid + 1, r, id, idx << 1 | 1, pos);
      tree[id].seg[idx] =
          min(tree[id << 1].seg[idx], tree[id << 1 | 1].seg[idx]);
    }
  }
  void modify(int l, int r, int x, int y, int val, int idx) {
    if (l == r) {
      tree[idx].modify(1, M, y, val, 1);
    } else {
      int mid = (l + r) >> 1;
      if (x <= mid)
        modify(l, mid, x, y, val, idx << 1);
      else
        modify(mid + 1, r, x, y, val, idx << 1 | 1);
      pushUp(1, M, idx, 1, y);
    }
  }
  int query(int l, int r, int xl, int xr, int yl, int yr, int idx) {
    if (l == xl && r == xr) return tree[idx].query(1, M, yl, yr, 1);
    int mid = (l + r) >> 1;
    if (xr <= mid)
      return query(l, mid, xl, xr, yl, yr, idx << 1);
    else if (xl > mid)
      return query(mid + 1, r, xl, xr, yl, yr, idx << 1 | 1);
    return min(query(l, mid, xl, mid, yl, yr, idx << 1),
               query(mid + 1, r, mid + 1, xr, yl, yr, idx << 1 | 1));
  }
} _2D_Tree[4];
vector<pair<int, int> > typePos[310 * 310];
vector<int> tmp;
int grid[310][310];
int main() {
  scanf("%d%d%d", &N, &M, &P);
  memset(_2D_Tree, 0x3f, sizeof _2D_Tree);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      scanf("%d", &grid[i][j]);
      typePos[grid[i][j]].push_back(pair<int, int>(i, j));
    }
  }
  typePos[0].push_back(pair<int, int>(1, 1));
  _2D_Tree[0].modify(1, N, 1, 1, -2, 1);
  _2D_Tree[1].modify(1, N, 1, 1, 0, 1);
  _2D_Tree[2].modify(1, N, 1, 1, 0, 1);
  _2D_Tree[3].modify(1, N, 1, 1, 2, 1);
  for (int type = 1; type <= P; type++) {
    tmp.clear();
    for (int i = 0; i < (int)typePos[type].size(); i++) {
      tmp.push_back(0x7fffffff);
      tmp[i] =
          min(tmp[i], typePos[type][i].first + typePos[type][i].second +
                          _2D_Tree[0].query(1, N, 1, typePos[type][i].first, 1,
                                            typePos[type][i].second, 1));
      tmp[i] =
          min(tmp[i], -typePos[type][i].first + typePos[type][i].second +
                          _2D_Tree[1].query(1, N, typePos[type][i].first, N, 1,
                                            typePos[type][i].second, 1));
      tmp[i] =
          min(tmp[i], typePos[type][i].first - typePos[type][i].second +
                          _2D_Tree[2].query(1, N, 1, typePos[type][i].first,
                                            typePos[type][i].second, M, 1));
      tmp[i] =
          min(tmp[i], -typePos[type][i].first - typePos[type][i].second +
                          _2D_Tree[3].query(1, N, typePos[type][i].first, N,
                                            typePos[type][i].second, M, 1));
    }
    for (int i = 0; i < (int)typePos[type - 1].size(); i++)
      for (int j = 0; j < 4; j++)
        _2D_Tree[j].modify(1, N, typePos[type - 1][i].first,
                           typePos[type - 1][i].second, 0x3f3f3f3f, 1);
    for (int i = 0; i < (int)typePos[type].size(); i++) {
      _2D_Tree[0].modify(
          1, N, typePos[type][i].first, typePos[type][i].second,
          -typePos[type][i].first - typePos[type][i].second + tmp[i], 1);
      _2D_Tree[1].modify(
          1, N, typePos[type][i].first, typePos[type][i].second,
          typePos[type][i].first - typePos[type][i].second + tmp[i], 1);
      _2D_Tree[2].modify(
          1, N, typePos[type][i].first, typePos[type][i].second,
          -typePos[type][i].first + typePos[type][i].second + tmp[i], 1);
      _2D_Tree[3].modify(
          1, N, typePos[type][i].first, typePos[type][i].second,
          typePos[type][i].first + typePos[type][i].second + tmp[i], 1);
    }
  }
  printf("%d\n", tmp[0]);
  return 0;
}
