#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  long long tot = 0;
  vector<vector<bool>> field(n, vector<bool>(n, true));
  for (int i = 0; i < (int)(n * n); i++) {
    int x;
    cin >> x;
    x--;
    int row = 0;
    while (x >= n) {
      row++;
      x -= n;
    }
    int up = 0, left = 0, right = 0, down = 0;
    int tmp = row;
    int cnt = 0;
    while (tmp - 1 >= 0) {
      if (field[tmp - 1][x] == true) cnt++;
      tmp--;
    }
    up = cnt;
    cnt = 0;
    tmp = row;
    while (tmp + 1 < n) {
      if (field[tmp + 1][x] == true) cnt++;
      tmp++;
    }
    down = cnt;
    cnt = 0;
    tmp = x;
    while (tmp - 1 >= 0) {
      if (field[row][tmp - 1] == true) cnt++;
      tmp--;
    }
    left = cnt;
    cnt = 0;
    tmp = x;
    while (tmp + 1 < n) {
      if (field[row][tmp + 1] == true) cnt++;
      tmp++;
    }
    right = cnt;
    tot += min({up, down, right, left});
    field[row][x] = false;
  }
  cout << tot << endl;
  return 0;
}
