#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
template <typename T, typename U>
istream& operator>>(istream& in, pair<T, U>& p) {
  in >> p.first >> p.second;
  return in;
}
struct BIT {
  vector<long long> bit;
  long long n;
  void init(long long size) {
    n = size;
    bit.resize(n + 1, 0);
  }
  long long sum(long long i) { return bit[i]; }
  void add(long long i, long long x) {
    for (long long j = i; j < n + 1; j++) {
      bit[j] += x;
    }
  }
};
signed main(signed argc, char* argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  const long long MOD = 1e9 + 7;
  long long n;
  cin >> n;
  vector<BIT> b1(n), b2(n);
  for (long long i = 0; i < n; i++) {
    b1[i].init(n);
    b2[i].init(n);
    for (long long j = 0; j < n; j++) {
      b1[i].add(j + 1, 1);
      b2[i].add(j + 1, 1);
    }
  }
  long long ans = 0;
  for (long long _ = 0; _ < n * n; _++) {
    long long p;
    cin >> p;
    p--;
    long long i = p / n, j = p % n;
    b1[j].add(i + 1, -1);
    b2[i].add(j + 1, -1);
    long long m = LLONG_MAX;
    if (i == 0 || i + 1 == n || j == 0 || j + 1 == n) m = 0;
    for (long long k = 0; k < i; k++) {
      long long m2 = b1[j].sum(i + 1);
      if (k) m2 -= b1[j].sum(k);
      long long m3;
      if (j && k) {
        m3 = b2[k].sum(j);
      } else
        m3 = 0;
      m3 = min(m3, b2[k].sum(n) - b2[k].sum(j + 1));
      m = min(m, m2 + m3);
    }
    for (long long k = i + 1; k < n; k++) {
      long long m2 = b1[j].sum(k + 1);
      if (i) m2 -= b1[j].sum(i);
      long long m3;
      if (j && k + 1 != n) {
        m3 = b2[k].sum(j);
      } else
        m3 = 0;
      m3 = min(m3, b2[k].sum(n) - b2[k].sum(j + 1));
      m = min(m, m2 + m3);
    }
    for (long long k = 0; k < j; k++) {
      long long m2 = b2[i].sum(j + 1);
      if (k) m2 -= b2[i].sum(k);
      long long m3;
      if (i && k) {
        m3 = b1[k].sum(i);
      } else
        m3 = 0;
      m3 = min(m3, b1[k].sum(n) - b1[k].sum(i + 1));
      m = min(m, m2 + m3);
    }
    for (long long k = j + 1; k < n; k++) {
      long long m2 = b2[i].sum(k + 1);
      if (j) m2 -= b2[i].sum(j);
      long long m3;
      if (i && k + 1 != n) {
        m3 = b1[k].sum(i);
      } else
        m3 = 0;
      m3 = min(m3, b1[k].sum(n) - b1[k].sum(i + 1));
      m = min(m, m2 + m3);
    }
    ans += m;
  }
  cout << ans << endl;
}
