#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<vector<int>> x, y;
bool isOK(vector<int>& v, int index, int key) {
  if (v[index] >= key)
    return true;
  else
    return false;
}
int bsearch(vector<int>& v, int key) {
  int ng = -1;
  int ok = (int)v.size();
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isOK(v, mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}
int n, p, result;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << std::setprecision(20);
  cin >> n;
  x.assign(n, vector<int>(0));
  y.assign(n, vector<int>(0));
  for (int i = 0; i < n * n; i++) {
    x[i / n].push_back(i);
    y[i % n].push_back(i);
  }
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    p--;
    auto&& col = x[p / n];
    auto&& row = y[p % n];
    auto&& itrx = lower_bound(col.begin(), col.end(), p);
    if (itrx == col.end()) itrx = col.begin();
    int ix = distance(col.begin(), itrx);
    auto&& itry = lower_bound(row.begin(), row.end(), p);
    if (itry == row.end()) itry = row.begin();
    int iy = distance(row.begin(), itry);
    result += min(
        ix, min(iy, min((int)col.size() - ix - 1, (int)row.size() - iy - 1)));
    col.erase(itrx);
    row.erase(itry);
  }
  cout << result;
}
