#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 5;
int sum_col[N], sum_row[N];
int pre_col[N][N], pre_row[N][N];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      pre_row[i][j] = j;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      pre_col[i][j] = i;
    }
  }
  for (int i = 1; i <= n; i++) sum_col[i] = n;
  for (int i = 1; i <= n; i++) sum_row[i] = n;
  int hate = 0;
  for (int i = 1; i <= n * n; i++) {
    int p;
    scanf("%d", &p);
    int row = p / n + 1;
    int col = p % n;
    if (p % n == 0) {
      row -= 1;
      col = n;
    }
    sum_col[col] -= 1;
    sum_row[row] -= 1;
    for (int j = col; j <= n; j++) {
      pre_row[row][j] -= 1;
    }
    for (int j = row; j <= n; j++) {
      pre_col[j][col] -= 1;
    }
    int val[] = {pre_col[row][col], pre_row[row][col],
                 sum_col[col] - pre_col[row][col],
                 sum_row[row] - pre_row[row][col]};
    hate += *min_element(val, val + 4);
  }
  printf("%d\n", hate);
  return 0;
}
