#include <bits/stdc++.h>
using namespace std;
long long n, p, now, row[505][505], col[505][505], x, y, ans;
pair<long long, long long> pos[500 * 500 + 5];
void addcol(long long idx, long long val, long long i) {
  for (; i <= n; i += i & (-i)) {
    col[idx][i] += val;
  }
}
void addrow(long long idx, long long val, long long i) {
  for (; i <= n; i += i & (-i)) {
    row[idx][i] += val;
  }
}
long long qrow(long long idx, long long i) {
  long long ret = 0;
  for (; i >= 1; i -= i & (-i)) {
    ret += row[idx][i];
  }
  return ret;
}
long long qcol(long long idx, long long i) {
  long long ret = 0;
  for (; i >= 1; i -= i & (-i)) {
    ret += col[idx][i];
  }
  return ret;
}
int main() {
  cin >> n;
  now = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      pos[now] = {i, j};
      now++;
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      addrow(i, 1, j);
      addcol(i, 1, j);
    }
  }
  for (long long i = 1; i <= n * n; i++) {
    cin >> p;
    x = pos[p].first;
    y = pos[p].second;
    ans += min(min(qrow(x, y - 1), qrow(x, n) - qrow(x, y)),
               min(qcol(y, x - 1), qcol(y, n) - qcol(y, x)));
    addrow(x, -1, y);
    addcol(y, -1, x);
  }
  cout << ans << endl;
}
