#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int b[505];
int sum;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  }
  for (int i = 1; i <= n * n; i++) {
    int h;
    cin >> h;
    int top1, top2;
    top1 = h / n + 1;
    top2 = h % n;
    if (top2 == 0) {
      top2 = n;
      top1--;
    }
    int q = 0, h1 = 0, z = 0, y = 0;
    for (int i = 1; i < top2; i++) {
      if (a[top1][i] == 1) {
        q++;
      }
    }
    for (int i = top2 + 1; i <= n; i++) {
      if (a[top1][i] == 1) {
        h1++;
      }
    }
    for (int i = 1; i < top1; i++) {
      if (a[i][top2] == 1) {
        z++;
      }
    }
    for (int i = top1 + 1; i <= n; i++) {
      if (a[i][top2] == 1) {
        y++;
      }
    }
    if (q && h1 && z && y) {
      sum += min(q, min(h1, min(z, y)));
    }
    a[top1][top2] = 0;
  }
  cout << sum;
  return 0;
}
