#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 3e5 + 5;
const int oo = 1e9 + 7;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dist[500][500];
bool ache[500][500];
void bfs(int n) {
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (dist[i][0] > 0) {
      dist[i][0] = 0;
      q.push(i * n + 0);
    }
    if (dist[i][n - 1] > 0) {
      dist[i][n - 1] = 0;
      q.push(i * n + n - 1);
    }
    if (dist[n - 1][i] > 0) {
      dist[n - 1][i] = 0;
      q.push((n - 1) * n + i);
    }
    if (dist[0][i] > 0) {
      dist[0][i] = 0;
      q.push(0 * n + i);
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int x = u / n;
    int y = u % n;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx < 0 or xx >= n) continue;
      if (yy < 0 or yy >= n) continue;
      if (dist[xx][yy] > dist[x][y] + 1) {
        dist[xx][yy] = dist[x][y] + 1;
        q.push(xx * n + yy);
      }
    }
  }
}
void upd(int x, int y, int n) {
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i];
    int yy = y + dx[i];
    if (xx < 0 or xx >= n) continue;
    if (yy < 0 or yy >= n) continue;
    if (dist[xx][yy] > dist[x][y] + ache[x][y]) {
      dist[xx][yy] = dist[x][y] + ache[x][y];
      upd(xx, yy, n);
    }
  }
}
void print(int n) {
  cerr << "line " << 79 << " : "
       << "dist"
       << " --> " << (dist) << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cout << dist[i][j] << " ";
    cout << endl;
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int n2 = n * n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) ache[i][j] = 1, dist[i][j] = oo;
  }
  bfs(n);
  int ans = 0;
  for (int i = 0; i < n2; i++) {
    int p;
    cin >> p;
    p--;
    int x = p / n;
    int y = p % n;
    ans += dist[x][y];
    ache[x][y] = 0;
    upd(x, y, n);
  }
  cout << ans << "\n";
}
