#include <bits/stdc++.h>
using namespace std;
int cost(vector<vector<bool>> vis, int r, int c) {
  int e = r + 1, w = r - 1, n = c - 1, s = c + 1, p1 = 0, p2 = 0, p3 = 0,
      p4 = 0;
  if (r == 0 || c == 0 || r == vis.size() - 1 || c == vis[0].size() - 1)
    return 0;
  while (e < vis.size()) {
    if (vis[e][c] == false) p1++;
    e++;
  }
  while (s < vis[0].size()) {
    if (vis[r][s] == false) p2++;
    s++;
  }
  while (n >= 0) {
    if (vis[r][n] == false) p3++;
    n--;
  }
  while (w >= 0) {
    if (vis[w][c] == false) p4++;
    w--;
  }
  return min(p1, min(p2, min(p3, p4)));
}
int main(int argc, char** argv) {
  int n;
  cin >> n;
  int a[n * n];
  vector<vector<bool>> arr(n, vector<bool>(n, false));
  for (int i = 0; i < n * n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int r = (a[i] - 1) / n;
    int c = (a[i] - 1) % n;
    arr[r][c] = true;
    ans += cost(arr, r, c);
  }
  cout << ans << endl;
}
