#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, a[N * N], c[N][N];
void ins(int x, int y, int w) {
  for (int i = x; i <= n; i += i & (-i))
    for (int j = y; j <= n; j += j & (-j)) c[i][j] += w;
}
int query(int x, int y) {
  int ans = 0;
  for (int i = x; i; i -= i & (-i))
    for (int j = y; j; j -= j & (-j)) ans += c[i][j];
  return ans;
}
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++) ins(i, j, 1);
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = (a[i] - 1) % n + 1;
    int w = n;
    ins(x, y, -1);
    w = min(w, query(x, y) - query(x - 1, y));
    w = min(w, query(x, y) - query(x, y - 1));
    w = min(w, query(n, y) - query(n, y - 1) - query(x - 1, y) +
                   query(x - 1, y - 1));
    w = min(w, query(x, n) - query(x - 1, n) - query(x, y - 1) +
                   query(x - 1, y - 1));
    ans += w;
  }
  printf("%d\n", ans);
  return 0;
}
