#include <bits/stdc++.h>
using namespace std;
const int N = 505;
struct tree {
  int t[N];
  void update(int id, int add) {
    while (id <= 500) {
      t[id] += add;
      id += id & (-id);
    }
  }
  int get(int id) {
    int ret = 0;
    while (id) {
      ret += t[id];
      id -= id & (-id);
    }
    return ret;
  }
} a[N * 2];
int n;
map<int, pair<long long, long long> > ma;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".inp", "r")) freopen(".inp", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      ma[(i - 1) * n + j] = make_pair(i, j);
      a[i].update(j, 1);
      a[j + n].update(i, 1);
    }
  long long ans = 0;
  for (int i = 1; i <= n * n; ++i) {
    int x;
    cin >> x;
    auto P = ma[x];
    int row = P.first, col = P.second;
    a[row].update(col, -1);
    a[col + n].update(row, -1);
    int valRow = a[row].get(col);
    int valRow2 = a[row].get(500) - valRow;
    int valCol = a[col + n].get(row);
    int valCol2 = a[col + n].get(500) - valCol;
    ans += min({valRow2, valRow, valCol2, valCol});
  }
  cout << ans;
  return 0;
}
