#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
vector<int> a[maxn];
vector<int> b[maxn];
void build(vector<int> &t, int i, int l, int r) {
  if (r - l == 1) {
    t[i] = 1;
    return;
  }
  int mid = (l + r) / 2;
  build(t, i * 2 + 1, l, mid);
  build(t, i * 2 + 2, mid, r);
  t[i] = t[i * 2 + 1] + t[i * 2 + 2];
}
int get(vector<int> &t, int i, int l, int r, int qL, int qR) {
  if (l >= qR || r <= qL) {
    return 0;
  }
  if (l >= qL && r <= qR) {
    return t[i];
  }
  int mid = (l + r) / 2;
  return get(t, i * 2 + 1, l, mid, qL, qR) + get(t, i * 2 + 2, mid, r, qL, qR);
}
void upd(vector<int> &t, int i, int l, int r, int pos, int val) {
  if (l > pos || r <= pos) {
    return;
  }
  if (r - l == 1) {
    t[i] = val;
    return;
  }
  int mid = (l + r) / 2;
  upd(t, i * 2 + 1, l, mid, pos, val);
  upd(t, i * 2 + 2, mid, r, pos, val);
  t[i] = t[i * 2 + 1] + t[i * 2 + 2];
}
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    a[i].resize(4 * n);
    build(a[i], 0, 0, n);
    b[i].resize(4 * n);
    build(b[i], 0, 0, n);
  }
  vector<int> need(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> need[i];
  }
  int ans = 0;
  for (int &now : need) {
    int i = (now - 1) / n;
    int j = now - i * n - 1;
    int ans_now = 1e9;
    ans_now = min(ans_now, get(a[i], 0, 0, n, j + 1, n));
    ans_now = min(ans_now, get(a[i], 0, 0, n, 0, j));
    ans_now = min(ans_now, get(b[j], 0, 0, n, i + 1, n));
    ans_now = min(ans_now, get(b[j], 0, 0, n, 0, i));
    ans += ans_now;
    upd(a[i], 0, 0, n, j, 0);
    upd(b[j], 0, 0, n, i, 0);
  }
  cout << ans;
  return 0;
}
