#include <bits/stdc++.h>
using namespace std;
int n;
int dis[505][505];
int occ[505][505];
pair<int, int> p[250005];
queue<pair<int, int> > q;
int ans = 0;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      dis[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), occ[i][j] = 1;
  for (int i = 0; i < n * n; ++i) {
    int X;
    cin >> X;
    --X;
    int x = X / n + 1, y = X % n + 1;
    ans += dis[x][y];
    occ[x][y] = 0;
    int nd =
        min(min(dis[x - 1][y] + occ[x - 1][y], dis[x + 1][y] + occ[x + 1][y]),
            min(dis[x][y + 1] + occ[x][y + 1], dis[x][y - 1] + occ[x][y - 1]));
    if (nd < dis[x][y]) {
      dis[x][y] = nd;
      q.push(make_pair(x, y));
      while (!q.empty()) {
        pair<int, int> cur = q.front();
        q.pop();
        int i = cur.first, j = cur.second;
        int nd = dis[i][j] + occ[i][j];
        if (nd < dis[i][j + 1]) dis[i][j + 1] = nd, q.push(make_pair(i, j + 1));
        if (nd < dis[i][j - 1]) dis[i][j - 1] = nd, q.push(make_pair(i, j - 1));
        if (nd < dis[i + 1][j]) dis[i + 1][j] = nd, q.push(make_pair(i + 1, j));
        if (nd < dis[i - 1][j]) dis[i - 1][j] = nd, q.push(make_pair(i - 1, j));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
