#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int c = 1;
  int a[n][n], arr[n * n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a[i][j] = c;
      c++;
    }
  }
  int cn = 0;
  for (int i = 0; i < (n * n); i++) {
    int row, col;
    int lf = 0, rt = 0, up = 0, dn = 0;
    int k;
    cin >> k;
    row = (k - 1) / n;
    col = k - 1 - (row * n);
    for (int j = 0; j < row; j++)
      if (a[j][col] != 0) up++;
    for (int j = (row + 1); j < n; j++)
      if (a[j][col] != 0) dn++;
    for (int j = 0; j < col; j++)
      if (a[row][j] != 0) lf++;
    for (int j = (col + 1); j < n; j++)
      if (a[row][j] != 0) rt++;
    int f = min(lf, min(rt, min(up, dn)));
    cn += f;
    a[row][col] = 0;
  }
  cout << cn << endl;
}
