#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF_LL = 1e18 + 3;
const int INF_int = 1e9 + 3;
int main() {
  int n;
  cin >> n;
  int arr[n * n];
  for (int i = 0; i < n * n; i++) cin >> arr[i];
  int field[n][n][4];
  bool absent[n][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      field[i][j][0] = i;
      field[i][j][1] = n - 1 - j;
      field[i][j][2] = n - 1 - i;
      field[i][j][3] = j;
      absent[i][j] = false;
    }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    int now = arr[i];
    int x = (now - 1) / n;
    int y = (now - 1) % n;
    int tmpans = min(
        {0, field[x][y][0], field[x][y][1], field[x][y][2], field[x][y][3]});
    int cnt = 0;
    for (int j = x - 1; j >= 0; j--) {
      if (!absent[j][y]) cnt++;
      tmpans = min(tmpans, cnt + min({0, field[j][y][0], field[j][y][1],
                                      field[j][y][2], field[j][y][3]}));
    }
    cnt = 0;
    for (int j = x + 1; j < n; j++) {
      if (!absent[j][y]) cnt++;
      tmpans = min(tmpans, cnt + min({0, field[j][y][0], field[j][y][1],
                                      field[j][y][2], field[j][y][3]}));
    }
    cnt = 0;
    for (int j = y - 1; j >= 0; j--) {
      if (!absent[x][j]) cnt++;
      tmpans = min(tmpans, cnt + min({0, field[x][j][0], field[x][j][1],
                                      field[x][j][2], field[x][j][3]}));
    }
    cnt = 0;
    for (int j = y + 1; j < n; j++) {
      if (!absent[x][j]) cnt++;
      tmpans = min(tmpans, cnt + min({0, field[x][j][0], field[x][j][1],
                                      field[x][j][2], field[x][j][3]}));
    }
    for (int j = x - 1; j >= 0; j--) {
      field[j][y][2]--;
    }
    for (int j = x + 1; j < n; j++) {
      field[j][y][0]--;
    }
    for (int j = y - 1; j >= 0; j--) {
      field[x][j][1]--;
    }
    for (int j = y + 1; j < n; j++) {
      field[x][j][3]--;
    }
    ans += tmpans;
    absent[x][y] = true;
  }
  cout << ans << endl;
}
