#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
using Pa = pair<T, U>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
int dp[4][510][510] = {};
class UnionFind {
 private:
  vec<int> p, s;
  int cnt;

 public:
  vec<int> val;
  UnionFind() {}
  UnionFind(int N) : cnt(N), s(N, 1), p(N), val(N) {
    iota(p.begin(), p.end(), 0);
  }
  int find(int x) {
    if (p[x] == x)
      return x;
    else
      return p[x] = find(p[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (s[x] > s[y]) swap(x, y);
    p[x] = y;
    s[y] += s[x];
    val[y] = min(val[y], val[x]);
    cnt--;
  }
  bool is_same_set(int x, int y) { return find(x) == find(y); }
  int size(int x) { return s[find(x)]; }
  int compnents_number() { return cnt; }
  int getval(int x) { return val[find(x)]; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vec<int> P(N * N);
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
  }
  vec<int> dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
  int inf = 1e9;
  vec<int> mi(N * N);
  vec<int> vis(N * N, 1);
  UnionFind uf(M);
  auto id = [&](int x, int y) { return x * N + y; };
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      mi[id(i, j)] = min({i, j, N - i - 1, N - j - 1});
      uf.val[id(i, j)] = mi[id(i, j)];
    }
  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };
  auto dist = [&](int a, int b) {
    int x1 = a / N, y1 = a % N, x2 = b / N, y2 = b % N;
    return abs(x1 - x2) + abs(y1 - y2);
  };
  ll ans = 0;
  for (int i = 0; i < M; i++) {
    vec<int> d(i + 1, inf), used(i + 1, 0);
    d[i] = 0;
    while (true) {
      int v = -1;
      int now = inf;
      for (int j = 0; j <= i; j++) {
        if (!used[j]) {
          if (v == -1 || now > d[j]) {
            v = j;
            now = d[j];
          }
        }
      }
      if (v == -1) break;
      used[v] = 1;
      for (int j = 0; j <= i; j++) {
        if (v != j) d[j] = min(d[j], d[v] + dist(P[v], P[j]) - 1);
      }
    }
    int res = inf;
    for (int j = 0; j <= i; j++) res = min(res, d[j] + mi[P[j]]);
    ans += res;
  }
  cout << ans << "\n";
}
