#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<bool> > v(n);
  for (int i = 0; i < (n); ++i) v[i].resize(n);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (n); ++j) v[i][j] = 1;
  ll ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    int x;
    cin >> x;
    --x;
    vector<ll> cnt(4, 0);
    ll cost = 1e9;
    for (int k = x; k > 0 && k / n; k -= n) {
      cnt[0] += v[k / n - 1][x % n];
    }
    for (int k = x; k < n * n - n; k += n) {
      cnt[1] += v[k / n + 1][x % n];
    }
    for (int k = x; k % n != 0; --k) {
      cnt[2] += v[x / n][k % n - 1];
    }
    for (int k = x; k % n; ++k) {
      cnt[3] += v[x / n][k % n + 1];
    }
    for (int k = 0; k < (4); ++k) {
      cost = min(cost, cnt[k]);
    }
    ans += cost;
    v[x / n][x % n] = 0;
  }
  cout << ans << endl;
}
