#include <bits/stdc++.h>
using namespace std;
const int inf1 = 2e9 + 9;
const long long inf2 = 8e18 + 9;
const int maxn = 5e2 + 9;
const int maxx = 2e5 + 9;
int n;
int r[maxn][maxn], c[maxn][maxn];
int sum_r(int i, int j) {
  int res = 0;
  while (j > 0) {
    res += r[i][j];
    j -= (j & (-j));
  }
  return res;
}
void add_r(int i, int j, int t) {
  while (j <= n) {
    r[i][j] += t;
    j += (j & (-j));
  }
}
int sum_c(int i, int j) {
  int res = 0;
  while (j > 0) {
    res += c[i][j];
    j -= (j & (-j));
  }
  return res;
}
void add_c(int i, int j, int t) {
  while (j <= n) {
    c[i][j] += t;
    j += (j & (-j));
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      add_r(i, j, 1);
      add_c(i, j, 1);
    }
  }
  int cnt = 0, t = n * n;
  while (t--) {
    int pos, i, j;
    cin >> pos;
    i = (pos - 1) / n + 1;
    j = pos - (i - 1) * n;
    cnt += min(min(sum_r(i, j - 1), sum_r(i, n) - sum_r(i, j)),
               min(sum_c(j, i - 1), sum_c(j, n) - sum_c(j, i)));
    add_r(i, j, -1);
    add_c(j, i, -1);
  }
  cout << cnt << endl;
}
