#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int s[maxn][maxn];
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) s[i][j] = 1;
  }
  for (int j = 1; j <= n * n; j++) {
    int l, r, u, d;
    l = r = u = d = 0;
    int p;
    cin >> p;
    int x = (p + n - 1) / n;
    int y = p - x * n + n;
    s[x][y] = 0;
    for (int i = 1; i < y; i++) {
      if (s[x][i] == 1) l++;
    }
    for (int i = y + 1; i <= n; i++) {
      if (s[x][i] == 1) r++;
    }
    for (int i = 1; i < x; i++) {
      if (s[i][y] == 1) u++;
    }
    for (int i = x + 1; i <= n; i++) {
      if (s[i][y] == 1) d++;
    }
    ans += min(min(l, r), min(u, d));
  }
  cout << ans << endl;
  return 0;
}
