#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int n;
  cin >> n;
  vector<vector<bool>> oc(n, vector<bool>(n, 1));
  long long ans = 0;
  for (int tt = 0; tt < (int)n * n; tt++) {
    int x;
    cin >> x;
    x--;
    int I = x / n, J = x % n;
    int mn = 50000;
    oc[I][J] = 0;
    for (int d = 0; d < (int)4; d++) {
      int cur = 0, i = I, j = J;
      while (i >= 0 && i < n && j >= 0 && j < n) {
        if (oc[i][j]) cur++;
        i += dx[d];
        j += dy[d];
      }
      mn = min(mn, cur);
    }
    ans += mn;
  }
  cout << ans << "\n";
}
