#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
template <class T>
void read(T& num) {
  char CH;
  bool F = false;
  for (CH = getchar(); CH < '0' || CH > '9'; F = CH == '-', CH = getchar())
    ;
  for (num = 0; CH >= '0' && CH <= '9';
       num = num * 10 + CH - '0', CH = getchar())
    ;
  F && (num = -num);
}
int stk[70], tp;
template <class T>
inline void print(T p) {
  if (!p) {
    puts("0");
    return;
  }
  while (p) stk[++tp] = p % 10, p /= 10;
  while (tp) putchar(stk[tp--] + '0');
  putchar('\n');
}
struct node {
  int al, ar;
  int mx;
};
struct node1 {
  int hl, hr;
  node subt[4000];
} t[4][310 * 4];
void build_sub(int id, int rt, int ll, int rr, int flag) {
  t[flag][id].subt[rt].al = ll;
  t[flag][id].subt[rt].ar = rr;
  t[flag][id].subt[rt].mx = inf;
  if (ll == rr) {
    return;
  }
  int mid = (ll + rr) >> 1;
  build_sub(id, rt << 1, ll, mid, flag);
  build_sub(id, rt << 1 | 1, mid + 1, rr, flag);
}
void build(int id, int l, int r, int ll, int rr, int flag) {
  t[flag][id].hl = l;
  t[flag][id].hr = r;
  build_sub(id, 1, ll, rr, flag);
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(id << 1, l, mid, ll, rr, flag);
  build(id << 1 | 1, mid + 1, r, ll, rr, flag);
}
void add_sub(int id, int rt, int act, int love, int flag) {
  t[flag][id].subt[rt].mx = min(love, t[flag][id].subt[rt].mx);
  if (t[flag][id].subt[rt].al == t[flag][id].subt[rt].ar) return;
  int mid = (t[flag][id].subt[rt].al + t[flag][id].subt[rt].ar) >> 1;
  if (act <= mid)
    add_sub(id, rt << 1, act, love, flag);
  else
    add_sub(id, rt << 1 | 1, act, love, flag);
  t[flag][id].subt[rt].mx =
      min(t[flag][id].subt[rt << 1].mx, t[flag][id].subt[rt << 1 | 1].mx);
}
void add(int id, int h, int act, int love, int flag) {
  add_sub(id, 1, act, love, flag);
  if (t[flag][id].hl == t[flag][id].hr) {
    return;
  }
  int mid = (t[flag][id].hl + t[flag][id].hr) >> 1;
  if (h <= mid)
    add(id << 1, h, act, love, flag);
  else
    add(id << 1 | 1, h, act, love, flag);
}
void add_sub1(int id, int rt, int act, int love, int flag) {
  t[flag][id].subt[rt].mx = love;
  if (t[flag][id].subt[rt].al == t[flag][id].subt[rt].ar) return;
  int mid = (t[flag][id].subt[rt].al + t[flag][id].subt[rt].ar) >> 1;
  if (act <= mid)
    add_sub1(id, rt << 1, act, love, flag);
  else
    add_sub1(id, rt << 1 | 1, act, love, flag);
  t[flag][id].subt[rt].mx = love;
}
void add1(int id, int h, int act, int love, int flag) {
  add_sub1(id, 1, act, love, flag);
  if (t[flag][id].hl == t[flag][id].hr) {
    return;
  }
  int mid = (t[flag][id].hl + t[flag][id].hr) >> 1;
  if (h <= mid)
    add1(id << 1, h, act, love, flag);
  else
    add1(id << 1 | 1, h, act, love, flag);
}
int sear(int id, int rt, int ll, int rr, int flag) {
  if (t[flag][id].subt[rt].al == ll && t[flag][id].subt[rt].ar == rr) {
    return t[flag][id].subt[rt].mx;
  }
  int mid = (t[flag][id].subt[rt].al + t[flag][id].subt[rt].ar) >> 1;
  if (rr <= mid)
    return sear(id, rt << 1, ll, rr, flag);
  else if (ll > mid)
    return sear(id, rt << 1 | 1, ll, rr, flag);
  else
    return min(sear(id, rt << 1, ll, mid, flag),
               sear(id, rt << 1 | 1, mid + 1, rr, flag));
}
int query(int id, int l, int r, int ll, int rr, int flag) {
  if (t[flag][id].hl == l && t[flag][id].hr == r) {
    return sear(id, 1, ll, rr, flag);
  }
  int mid = (t[flag][id].hl + t[flag][id].hr) >> 1;
  if (r <= mid)
    return query(id << 1, l, r, ll, rr, flag);
  else if (l > mid)
    return query(id << 1 | 1, l, r, ll, rr, flag);
  else
    return min(query(id << 1, l, mid, ll, rr, flag),
               query(id << 1 | 1, mid + 1, r, ll, rr, flag));
}
struct no {
  int l, r, dis;
};
vector<no> ve[310 * 310];
int a[305][305], le[310 * 310];
int n, m, p;
int main() {
  no x;
  read(n);
  read(m);
  read(p);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      read(a[i][j]);
      x.l = i;
      x.r = j;
      x.dis = inf;
      ve[a[i][j]].push_back(x);
    }
  }
  for (int i = 0; i < 4; i++) {
    build(1, 1, n, 1, m, i);
  }
  for (int i = 1; i <= p; i++) {
    le[i] = ve[i].size();
  }
  for (int i = 0; i < le[1]; i++) {
    int l = ve[1][i].l, r = ve[1][i].r;
    ve[1][i].dis = l + r - 2;
  }
  for (int i = 2; i <= p; i++) {
    int len1 = le[i], len2 = le[i - 1];
    for (int j = 0; j < len2; j++) {
      int l = ve[i - 1][j].l, r = ve[i - 1][j].r;
      add(1, l, r, ve[i - 1][j].dis - l - r, 0);
      add(1, l, r, ve[i - 1][j].dis - l + r, 1);
      add(1, l, r, ve[i - 1][j].dis + l - r, 2);
      add(1, l, r, ve[i - 1][j].dis + l + r, 3);
    }
    for (int j = 0; j < len1; j++) {
      int l = ve[i][j].l, r = ve[i][j].r;
      ve[i][j].dis = min(ve[i][j].dis, query(1, 1, l, 1, r, 0) + l + r);
      ve[i][j].dis = min(ve[i][j].dis, query(1, 1, l, r, m, 1) + l - r);
      ve[i][j].dis = min(ve[i][j].dis, query(1, l, n, 1, r, 2) - l + r);
      ve[i][j].dis = min(ve[i][j].dis, query(1, l, n, r, m, 3) - l - r);
    }
    for (int j = 0; j < len2; j++) {
      add1(1, ve[i - 1][j].l, ve[i - 1][j].r, inf, 0);
      add1(1, ve[i - 1][j].l, ve[i - 1][j].r, inf, 1);
      add1(1, ve[i - 1][j].l, ve[i - 1][j].r, inf, 2);
      add1(1, ve[i - 1][j].l, ve[i - 1][j].r, inf, 3);
    }
  }
  cout << ve[p][0].dis << endl;
  return 0;
}
