#include <bits/stdc++.h>
using namespace std;
int a[500][500], n, v[250000], viewers;
int moveLeftRight(int i, int j) {
  int left = 0, right = 0;
  for (int k = 1; k <= n; k++) {
    if (k < j && a[i][k] != 0)
      left++;
    else if (k > j && a[i][k] != 0)
      right++;
  }
  if (left < right) return left;
  return right;
}
int moveUpDown(int i, int j) {
  int up = 0, down = 0;
  for (int k = 1; k <= n; k++) {
    if (k < i && a[k][j] != 0)
      up++;
    else if (k > i && a[k][j] != 0)
      down++;
  }
  if (up < down) return up;
  return down;
}
int main() {
  cin >> n;
  int nr = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = nr++;
  for (int i = 0; i < n * n; i++) {
    cin >> v[i];
    int line = v[i] / n;
    int column = v[i] % n;
    if (column > 0)
      line++;
    else
      column = n;
    viewers += min(moveUpDown(line, column), moveLeftRight(line, column));
    a[line][column] = 0;
  }
  cout << viewers;
  return 0;
}
