#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int segment[maxn * 2][maxn * 4] = {0};
void build(int row, int i, int l, int r) {
  if (l == r) {
    segment[row][i] = 1;
    return;
  }
  int m = (l + r) / 2;
  build(row, i << 1, l, m);
  build(row, i << 1 | 1, m + 1, r);
  segment[row][i] = segment[row][i << 1] + segment[row][i << 1 | 1];
}
void update(int row, int i, int l, int r, int pos) {
  if (l == r) {
    segment[row][i] = 0;
    return;
  }
  int m = (l + r) / 2;
  if (m >= pos)
    update(row, i << 1, l, m, pos);
  else
    update(row, i << 1 | 1, m + 1, r, pos);
  segment[row][i] = segment[row][i << 1] + segment[row][i << 1 | 1];
}
int query(int row, int i, int l, int r, int x, int y) {
  if (l >= x && r <= y) {
    return segment[row][i];
  }
  int m = (l + r) / 2;
  int ans = 0;
  if (m >= x) ans += query(row, i << 1, l, m, x, y);
  if (m < y) ans += query(row, i << 1 | 1, m + 1, r, x, y);
  return ans;
}
int a[maxn * maxn] = {0};
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> a[i];
  for (int i = 1; i <= 2 * n; i++) build(i, 1, 1, n);
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int row = a[i] % n;
    if (row == 0) row = n;
    int col = (a[i] - 1) / n + 1;
    update(row, 1, 1, n, col);
    update(col + n, 1, 1, n, row);
    ans +=
        min({query(row, 1, 1, n, 1, col), query(row, 1, 1, n, col, n),
             query(col + n, 1, 1, n, 1, row), query(col + n, 1, 1, n, row, n)});
  }
  cout << ans << endl;
  return 0;
}
