#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5;
int _w;
int n;
struct BIT {
  int tree[N];
  inline void add(int i, int v) {
    while (i <= n) tree[i] += v, i += (i & (-i));
  }
  inline int sum(int i) {
    int res = 0;
    while (i) res += tree[i], i &= i - 1;
    return res;
  }
  inline int qry(int l, int r) { return sum(r) - sum(l - 1); }
} R[N], C[N];
int m, x, y;
long long ans;
int main(void) {
  _w = scanf("%d", &n);
  m = n * n;
  for (int i = 1, id, a, b, c, d; i <= m; ++i) {
    _w = scanf("%d", &id);
    y = (id - 1) % n + 1;
    x = (id - 1) / n + 1;
    R[y].add(x, -1);
    C[x].add(y, -1);
    a = R[y].qry(1, x) + x;
    b = R[y].qry(x, n) + n - x + 1;
    c = C[x].qry(1, y) + y;
    d = C[x].qry(y, n) + n - y + 1;
    ans += min(min(a, b), min(c, d));
  }
  printf("%lld", ans);
  return 0;
}
