#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

#define popCnt(x) (__builtin_popcountll(x))

typedef long long Long;
const int N = 505;
int dist[N][N];
bool vis[N][N];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void dfs(int i, int j, int d) {
  if (d >= dist[i][j]) return;
  dist[i][j] = d;
  d += !vis[i][j];
  for (int x = 0; x < 4; ++x) {
    dfs(i + dx[x], j + dy[x], d);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      dist[i][j] = min(min(i - 1, j - 1), min(n - i, n - j));
    }
  }
  for (int i = 0; i <= n + 1; ++i) {
    vis[0][i] = vis[n + 1][i] = vis[i][0] = vis[i][n + 1] = true;
  }
  int res = 0;
  for (int i = 1; i <= n * n; ++i) {
    int c;
    cin >> c;
    --c;
    int x = c / n + 1, y = c % n + 1;
    res += dist[x][y];
    vis[x][y] = true;
    for (int d = 0; d < 4; ++d) {
      dfs(x + dx[d], y + dy[d],
          dist[x + dx[d ^ 1]][y + dy[d ^ 1]] +
              !vis[x + dx[d ^ 1]][y + dy[d ^ 1]]);
    }
  }
  cout << res << endl;

  return 0;
}