#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int main(int, char**) {
  int n;
  cin >> n;
  vector<vector<int> > cost(n, vector<int>(n));
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) {
      cost[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
    }
  vector<vector<bool> > pleft(n, vector<bool>(n, false));
  ll ans = 0;
  for (int _ = 0; _ < (n * n); _++) {
    int ps;
    cin >> ps;
    ps--;
    int sx = ps / n;
    int sy = ps % n;
    ans += cost[sx][sy];
    pleft[sx][sy] = true;
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    vector<vector<bool> > done(n, vector<bool>(n, false));
    queue<P> q;
    q.push(P(sx, sy));
    done[sx][sy] = true;
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      for (int i = 0; i < (4); i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if (done[nx][ny]) continue;
        if (cost[nx][ny] == 0) continue;
        if (pleft[x][y]) {
          cost[nx][ny] = min(cost[nx][ny], cost[x][y]);
        } else {
          cost[nx][ny] = min(cost[nx][ny], cost[x][y] + 1);
        }
        q.push(P(nx, ny));
        done[nx][ny] = true;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
