#include <bits/stdc++.h>
using namespace std;
int n, a[510 * 510], val[510][510];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n * n; i++) cin >> a[i];
  for (int i = n * n; i >= 1; i--) {
    int x = a[i] % n;
    if (x == 0) x = n;
    int y = (a[i] - x) / n + 1;
    int res = 0x3f3f3f3f, cnt;
    cnt = 0;
    for (int j = x + 1; j <= n; j++) cnt += val[j][y];
    res = min(res, cnt);
    cnt = 0;
    for (int j = 1; j <= x - 1; j++) cnt += val[j][y];
    res = min(res, cnt);
    cnt = 0;
    for (int j = y + 1; j <= n; j++) cnt += val[x][j];
    res = min(res, cnt);
    cnt = 0;
    for (int j = 1; j <= y - 1; j++) cnt += val[x][j];
    res = min(res, cnt);
    sum += 1ll * res;
    val[x][y] = 1;
  }
  cout << sum << endl;
  return 0;
}
