#include <bits/stdc++.h>
using namespace std;
const long long asize = 505;
long long n, fth[asize][asize], ftv[asize][asize];
void update(long long ft[], long long p, long long v) {
  for (; p <= n; p += (p & -p)) ft[p] += v;
  return;
}
long long query(long long ft[], long long p) {
  long long ans = 0;
  for (; p > 0; p -= (p & -p)) ans += ft[p];
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL);
  long long k, n2, r, h, c, ans;
  cin >> n;
  n2 = n * n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      update(fth[i], j, 1);
      update(ftv[i], j, 1);
    }
  }
  long long sum = 0;
  for (int i = 0; i < n2; i++) {
    cin >> k;
    r = ((k - 1) / n) + 1;
    c = k % n;
    if (c == 0) c = n;
    ans = INT_MAX;
    ans = min(ans, query(fth[r], c - 1));
    ans = min(ans, query(fth[r], n) - query(fth[r], c));
    ans = min(ans, query(ftv[c], r - 1));
    ans = min(ans, query(ftv[c], n) - query(ftv[c], r));
    update(fth[r], c, -1);
    update(ftv[c], r, -1);
    sum += ans;
  }
  cout << sum;
  return 0;
}
