#include <bits/stdc++.h>
using namespace std;
long long power(long long x, unsigned long long y, unsigned long long m) {
  if (y == 0) return 1;
  long long p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
long long modInverse(long long a, long long m) { return power(a, m - 2, m); }
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long t = 1;
  while (t-- > 0) {
    long long n = 0;
    cin >> n;
    vector<vector<long long>> v(n);
    long long visit[n * n + 1];
    for (long long i = 0; i < n; ++i) {
      for (long long j = 1; j <= n; j++) {
        v[i].push_back((n * i) + j);
        visit[i * n + j] = 0;
      }
    }
    vector<long long> order;
    long long a = 0, ans = 0;
    for (long long i = 0; i < n * n; ++i) {
      cin >> a;
      long long x = (a - 1) / (n);
      long long y = a - 1 - n * x;
      long long m = 1e18, temp = 0;
      visit[a] = 1;
      for (long long j = 0; j < y; j++) {
        if (visit[v[x][j]] == 0) temp++;
      }
      m = min(m, temp);
      temp = 0;
      for (long long j = y + 1; j < n; j++) {
        if (visit[v[x][j]] == 0) temp++;
      }
      m = min(m, temp);
      temp = 0;
      for (long long j = 0; j < x; j++) {
        if (visit[v[j][y]] == 0) temp++;
      }
      m = min(m, temp);
      temp = 0;
      for (long long j = x + 1; j < n; j++) {
        if (visit[v[j][y]] == 0) temp++;
      }
      m = min(m, temp);
      temp = 0;
      ans += m;
    }
    cout << ans;
  }
  return 0;
}
