#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
int hang[N][N], lie[N][N], toth[N], totl[N], ans, n;
struct point {
  int x, y, id;
} a[N * N];
int lb(int x) { return x & (-x); }
bool cmp(point x, point y) { return x.id < y.id; }
void addh(int pos, int x, int val) {
  while (x <= n) {
    hang[pos][x] += val;
    x += lb(x);
  }
}
void addl(int pos, int x, int val) {
  while (x <= n) {
    lie[pos][x] += val;
    x += lb(x);
  }
}
int askl(int pos, int x) {
  int res = 0;
  while (x) {
    res += lie[pos][x];
    x -= lb(x);
  }
  return res;
}
int askh(int pos, int x) {
  int res = 0;
  while (x) {
    res += hang[pos][x];
    x -= lb(x);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int tmp = 0;
      scanf("%d", &tmp);
      a[++cnt].x = (tmp - 1) / n + 1;
      a[cnt].y = tmp - ((tmp - 1) / n) * n;
    }
    toth[i] = totl[i] = n;
  }
  for (int i = 1; i <= cnt; i++) {
    int now1 = askh(a[i].x, a[i].y);
    int now2 = askl(a[i].y, a[i].x);
    int num1 = min((a[i].y - 1 - now1), toth[a[i].x] - (a[i].y - 1 - now1) - 1);
    int num2 = min((a[i].x - 1 - now2), totl[a[i].y] - (a[i].x - 1 - now2) - 1);
    ans += min(num1, num2);
    addh(a[i].x, a[i].y, 1);
    addl(a[i].y, a[i].x, 1);
    toth[a[i].x]--;
    totl[a[i].y]--;
  }
  cout << ans << endl;
}
