#include <bits/stdc++.h>
using namespace std;
const int kInf = numeric_limits<int>::max() / 32;
struct Item {
  int x, value, left, right;
};
void MarkLeft(vector<Item>& items) {
  int N = items.size();
  items[0].left = items[0].value;
  for (int i = 1; i < N; ++i) {
    items[i].left =
        min(items[i].value, items[i - 1].left + items[i].x - items[i - 1].x);
  }
}
void MarkRight(vector<Item>& items) {
  int N = items.size();
  items[N - 1].right = items[N - 1].value;
  for (int i = N - 2; i >= 0; --i) {
    items[i].right =
        min(items[i].value, items[i + 1].right + items[i + 1].x - items[i].x);
  }
}
int CalcImpact(int x, const vector<Item>& items) {
  Item to_search = {x, -1, -1, -1};
  int i = lower_bound(
              items.begin(), items.end(), to_search,
              [](const Item& lhs, const Item& rhs) { return lhs.x < rhs.x; }) -
          items.begin();
  if (i < items.size() && items[i].x == x) {
    return min(items[i].left, items[i].right);
  }
  int result = kInf;
  if (i > 0) {
    result = items[i - 1].left + abs(x - items[i - 1].x);
  }
  if (i < items.size()) {
    result = min(result, items[i].right + abs(x - items[i].x));
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M, P;
  cin >> N >> M >> P;
  vector<map<int, vector<Item>>> items(P);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int a;
      cin >> a;
      --a;
      items[a][i].push_back({j, -1, -1, -1});
    }
  }
  for (auto& a : items) {
    for (auto& p : a) {
      vector<Item>& v = p.second;
      sort(v.begin(), v.end(),
           [](const Item& lhs, const Item& rhs) { return lhs.x < rhs.x; });
    }
  }
  {
    assert(items[P - 1].size() == 1);
    auto& i = items[P - 1].begin()->second;
    assert(i.size() == 1);
    i[0].value = i[0].left = i[0].right = 0;
  }
  for (int p = P - 2; p >= 0; --p) {
    auto& current = items[p];
    const auto& next = items[p + 1];
    for (auto& pr : current) {
      int x = pr.first;
      for (Item& v : pr.second) {
        int result = kInf;
        for (const auto& pr2 : next) {
          int x2 = pr2.first;
          const vector<Item>& v2 = pr2.second;
          result = min(result, abs(x2 - x) + CalcImpact(v.x, v2));
        }
        v.value = result;
      }
      MarkLeft(pr.second);
      MarkRight(pr.second);
    }
  }
  int result = kInf;
  for (const auto& p : items[0]) {
    int x = p.first;
    for (const Item& i : p.second) {
      result = min(result, x + i.x + i.value);
    }
  }
  cout << result << '\n';
  return 0;
}
