#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, a[N][N], x, ans;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  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++) {
    cin >> x;
    int r = (x - 1) / n + 1;
    int c = x - (r - 1) * n;
    int top = 0;
    for (int j = 1; j < r; j++) {
      top += a[j][c];
    }
    int bottom = 0;
    for (int j = n; j > r; j--) {
      bottom += a[j][c];
    }
    int left = 0;
    for (int j = 1; j < c; j++) {
      left += a[r][j];
    }
    int right = 0;
    for (int j = n; j > c; j--) {
      right += a[r][j];
    }
    ans += min(min(top, bottom), min(left, right));
    a[r][c] = 0;
  }
  cout << ans << "\n";
}
