#include <bits/stdc++.h>
using namespace std;
char a[502][502];
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n + 2; i++)
    a[i][0] = a[0][i] = a[n + 1][i] = a[i][n + 1] = -1;
  for (int i = 0; i < n * n; i++) {
    int x;
    cin >> x;
    int l = (x - 1) / n + 1, c = (x - 1) % n + 1;
    long long ansx = 1LL << 62;
    int ll, cc;
    ll = l;
    cc = c;
    a[l][c] = 1;
    long long nr = 0;
    while (a[ll][cc] != -1) {
      if (a[ll][cc] == 0) nr++;
      ll--;
    }
    ansx = min(nr, ansx);
    ll = l;
    cc = c;
    nr = 0;
    while (a[ll][cc] != -1) {
      if (a[ll][cc] == 0) nr++;
      ll++;
    }
    ansx = min(nr, ansx);
    ll = l;
    cc = c;
    nr = 0;
    while (a[ll][cc] != -1) {
      if (a[ll][cc] == 0) nr++;
      cc--;
    }
    ansx = min(nr, ansx);
    ll = l;
    cc = c;
    nr = 0;
    while (a[ll][cc] != -1) {
      if (a[ll][cc] == 0) nr++;
      cc++;
    }
    ansx = min(nr, ansx);
    ans += ansx;
  }
  cout << ans;
  return 0;
}
