#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 520;
int dis[N][N];
pair<int, int> nod[N * N];
int n;
int vis[N][N];
int upd(int v, int u, int val) {
  if (v > n || v < 1 || u > n || u < 1) return 0;
  if (dis[v][u] > val) {
    dis[v][u] = val;
    return 1;
  }
  return 0;
}
int solve(int x, int y) {
  dis[x][y]--;
  queue<pair<int, int> > q;
  q.push({x, y});
  while (q.size()) {
    int v = q.front().first, u = q.front().second;
    q.pop();
    int z = dis[v][u] + vis[v + 1][u];
    int b = upd(v + 1, u, z);
    if (b) {
      q.push({v + 1, u});
    }
    z = dis[v][u] + vis[v - 1][u];
    b = upd(v - 1, u, z);
    if (b) {
      q.push({v - 1, u});
    }
    z = dis[v][u] + vis[v][u + 1];
    b = upd(v, u + 1, z);
    if (b) {
      q.push({v, u + 1});
    }
    z = dis[v][u] + vis[v][u - 1];
    b = upd(v, u - 1, z);
    if (b) {
      q.push({v, u - 1});
    }
  }
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int cnt = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j))) + 1;
      nod[cnt++] = {i, j};
      vis[i][j] = 1;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int x;
    cin >> x;
    int v = nod[x].first, u = nod[x].second;
    solve(v, u);
    ans += dis[v][u];
  }
  cout << ans << '\n';
}
