#include <bits/stdc++.h>
using namespace std;
const int nm = 502;
int n, a[nm * nm];
int rtree[nm][nm], ctree[nm][nm];
void update(int* tree, int i, int x) {
  while (i <= n) {
    tree[i] += x;
    i += i & (-i);
  }
}
void update(int i, int j, int x) {
  update(rtree[i], j, x);
  update(ctree[j], i, x);
}
int get(int* tree, int i) {
  int res = 0;
  while (i > 0) {
    res += tree[i];
    i -= i & (-i);
  }
  return res;
}
int getrow(int i, int j) { return get(rtree[i], j); }
int getcol(int i, int j) { return get(ctree[j], i); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      update(i, j, 1);
    }
  }
  int res = 0;
  for (int i = 1; i <= n * n; ++i) {
    int u = (a[i] - 1) / n + 1;
    int v = (a[i] - 1) % n + 1;
    int x = min(getrow(u, v - 1), getrow(u, n) - getrow(u, v));
    int y = min(getcol(u - 1, v), getcol(n, v) - getcol(u, v));
    res += min(x, y);
    update(u, v, -1);
  }
  printf("%d\n", res);
}
