#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int calcTopSeatNum(int *seat, int n, int j) {
  int seatedNum = 0;
  while (j > n) {
    j -= n;
    if (seat[j]) {
      seatedNum += 1;
    }
  }
  return seatedNum;
}
int calcLeftSeatNum(int *seat, int n, int j) {
  int seatedNum = 0;
  while (j % n != 1) {
    j -= 1;
    if (seat[j]) {
      seatedNum += 1;
    }
  }
  return seatedNum;
}
int calcRightSeatNum(int *seat, int n, int j) {
  int seatedNum = 0;
  while (j % n != 0) {
    j += 1;
    if (seat[j]) {
      seatedNum += 1;
    }
  }
  return seatedNum;
}
int calcBottomSeatNum(int *seat, int n, int j) {
  int seatedNum = 0;
  while (j + n <= n * n) {
    j += n;
    if (seat[j]) {
      seatedNum += 1;
    }
  }
  return seatedNum;
}
int calcMinSeatNum(int *seat, int n, int j) {
  int minVal = calcTopSeatNum(seat, n, j);
  if (minVal > calcLeftSeatNum(seat, n, j)) {
    minVal = calcLeftSeatNum(seat, n, j);
  }
  if (minVal > calcRightSeatNum(seat, n, j)) {
    minVal = calcRightSeatNum(seat, n, j);
  }
  if (minVal > calcBottomSeatNum(seat, n, j)) {
    minVal = calcBottomSeatNum(seat, n, j);
  }
  return minVal;
}
int main() {
  int n;
  int *pa;
  int *seat;
  cin >> n;
  int ans = 0;
  pa = (int *)malloc(sizeof(int) * n * n);
  seat = (int *)malloc(sizeof(int) * (n * n + 1));
  for (int i = 0; i < (n * n); ++i) {
    cin >> pa[i];
  }
  for (int i = 0; i < (n * n); ++i) {
    int j = pa[n * n - 1 - i];
    int minseatnum = calcMinSeatNum(seat, n, j);
    ans += minseatnum;
    seat[j] = 1;
  }
  cout << ans << endl;
  return 0;
}
