#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int N = 1e5;
const int MOD = 998244353;
const double EPS = 1e-9;
const double PI = acos(-1);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int group[300000];
int t[501][501];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  int p[n * n];
  int a[n + 1][n + 1];
  int inc = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = inc++;
  for (int &x : p) cin >> x;
  int last_group = 1;
  int ans = 0;
  for (int cur : p) {
    int y = (cur % n ? cur % n : n), x = (cur + n - 1) / n;
    int mn = n;
    mn = min(mn, x - 1);
    mn = min(mn, n - x);
    mn = min(mn, y - 1);
    mn = min(mn, n - y);
    int sat = 0;
    for (int col = x - 1; col >= 1; col--) {
      if (t[y][col] == 0)
        sat++;
      else
        mn = min(mn, sat + group[t[y][col]]);
    }
    sat = 0;
    for (int col = x + 1; col <= n; col++) {
      if (t[y][col] == 0)
        sat++;
      else
        mn = min(mn, sat + group[t[y][col]]);
    }
    sat = 0;
    for (int row = y - 1; row >= 1; row--) {
      if (t[row][x] == 0)
        sat++;
      else
        mn = min(mn, sat + group[t[row][x]]);
    }
    sat = 0;
    for (int row = y + 1; row <= n; row++) {
      if (t[row][x] == 0)
        sat++;
      else
        mn = min(mn, sat + group[t[row][x]]);
    }
    bool noGroups = 1;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
        if (t[xx][yy] != 0) {
          noGroups = 0;
          mn = min(mn, group[t[xx][yy]]);
        }
      }
    }
    if (noGroups) {
      t[x][y] = last_group++;
      group[t[x][y]] = mn;
    } else {
      for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
          if (t[xx][yy] != 0) {
            group[t[xx][yy]] = mn;
            t[x][y] = t[xx][yy];
          }
        }
      }
    }
    ans += mn;
  }
  cout << ans;
}
