#include <bits/stdc++.h>
using namespace std;
int hate(vector<vector<int>>& arr, int r, int c, int n) {
  int i = r, j = c, tot = 0, res = n;
  arr[r][c] = -1;
  while (i >= 0) {
    if (arr[i][j] >= 0) {
      tot++;
    }
    i--;
  }
  res = min(res, tot);
  if (tot == 0) {
    return 0;
  }
  i = r, tot = 0;
  while (j >= 0) {
    if (arr[i][j] >= 0) {
      tot++;
    }
    j--;
  }
  res = min(res, tot);
  if (tot == 0) {
    return 0;
  }
  j = c, tot = 0;
  while (j < n) {
    if (arr[i][j] >= 0) {
      tot++;
    }
    j++;
  }
  res = min(res, tot);
  if (tot == 0) {
    return 0;
  }
  j = c, tot = 0;
  while (i < n) {
    if (arr[i][j] >= 0) {
      tot++;
    }
    i++;
  }
  res = min(res, tot);
  if (tot == 0) {
    return 0;
  }
  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;
}
