#include <bits/stdc++.h>
using namespace std;
const int N = 555;
int a[N * N];
int vis[N][N];
int n, m, k, tot;
int put(int s) {
  int x = (s - 1) / n + 1;
  int y = s - (x - 1) * n;
  int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
  vis[x][y] = 1;
  for (int i = 1; i < x; i++) sum1 = sum1 + (vis[i][y] ^ 1);
  for (int i = x + 1; i <= n; i++) sum2 = sum2 + (vis[i][y] ^ 1);
  for (int i = 1; i < y; i++) sum3 = sum3 + (vis[x][i] ^ 1);
  for (int i = y + 1; i <= n; i++) sum4 = sum4 + (vis[x][i] ^ 1);
  return min(min(sum1, sum2), min(sum3, sum4));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i <= n * n; i++) ans = ans + put(a[i]);
  printf("%d\n", ans);
  return 0;
}
