#include <bits/stdc++.h>
using namespace std;
long long hate(vector<vector<int>>& arr, int r, int c, int n) {
  int u = r, d = r, l = c, w = c;
  long long t1 = 0, t2 = 0, t3 = 0, t4 = 0;
  arr[r][c] = -1;
  while (u >= 0) {
    if (arr[u][c] >= 0) {
      t1++;
    }
    u--;
  }
  while (d < n) {
    if (arr[d][c] >= 0) {
      t2++;
    }
    d++;
  }
  while (l >= 0) {
    if (arr[r][l] >= 0) {
      t3++;
    }
    l--;
  }
  while (w < n) {
    if (arr[r][w] >= 0) {
      t4++;
    }
    w++;
  }
  long long res = min(t1, t2);
  res = min(res, t3);
  res = min(res, t4);
  return res;
}
int main() {
  int n;
  cin >> n;
  int order[n * n];
  for (int i = 0; i < n * n; i++) {
    cin >> order[i];
    order[i]--;
  }
  vector<vector<int>> arr;
  int y = 0;
  for (int i = 0; i < n; i++) {
    vector<int> m;
    for (int j = 0; j < n; j++) {
      m.push_back(y);
      y++;
    }
    arr.push_back(m);
  }
  long long total = 0;
  for (int i = 0; i < n * n; i++) {
    int val = order[i];
    int r = val / n, c = val % n;
    total += hate(arr, r, c, n);
  }
  cout << total;
  return 0;
}
