#include <bits/stdc++.h>
using namespace std;
int n;
int bit[2][505][505];
void update(int x, int y, int add) {
  int k = y;
  while (k <= n) {
    bit[0][x][k] += add;
    k += k & (-k);
  }
  k = x;
  while (k <= n) {
    bit[1][y][k] += add;
    k += k & (-k);
  }
}
struct S {
  int l, r, u, d;
};
int query(int x, int y) {
  S ret;
  update(x, y, -1);
  ret.l = 0;
  int k = y;
  while (k > 0) {
    ret.l += bit[0][x][k];
    k -= k & (-k);
  }
  ret.r = -ret.l;
  k = n;
  while (k > 0) {
    ret.r += bit[0][x][k];
    k -= k & (-k);
  }
  k = x;
  ret.u = 0;
  while (k > 0) {
    ret.u += bit[1][y][k];
    k -= k & (-k);
  }
  k = n;
  ret.d = -ret.u;
  while (k > 0) {
    ret.d += bit[1][y][k];
    k -= k & (-k);
  }
  int res = min(ret.l, ret.r);
  res = min(res, ret.u);
  res = min(res, ret.d);
  return res;
}
int main() {
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) update(i, j, 1);
  for (int i = 1; i <= n * n; i++) {
    int now;
    scanf("%d", &now);
    now--;
    int x = now / n + 1;
    int y = now % n + 1;
    int add = query(x, y);
    ans += add;
  }
  cout << ans << endl;
  return 0;
}
