#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  long long a[n][n];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) a[i][j] = 1;
  vector<long long> v(n * n);
  for (long long i = 0; i < n * n; i++) cin >> v[i];
  long long res = 0;
  for (long long i = 0; i < n * n; i++) {
    long long r = (v[i] - 1) / n;
    long long c = v[i] - n * r - 1;
    long long m = n;
    long long co = 0;
    for (long long j = r + 1; j < n; j++) {
      if (a[j][c]) co++;
    }
    m = min(m, co);
    co = 0;
    for (long long j = r - 1; j >= 0; j--) {
      if (a[j][c]) co++;
    }
    m = min(m, co);
    co = 0;
    for (long long j = c - 1; j >= 0; j--) {
      if (a[r][j]) co++;
    }
    m = min(m, co);
    co = 0;
    for (long long j = c + 1; j < n; j++) {
      if (a[r][j]) co++;
    }
    m = min(m, co);
    res += m;
    a[r][c] = 0;
  }
  cout << res;
  return 0;
}
