#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

  int n;
  cin >> n;
  
  vector<vector<int>> seat(n, vector<int> (n, 1)), val = seat;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      val[i][j] = min({i, j, n-i-1, n-j-1});
    }
  }
  
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  function<void(int, int, int)> update = [&](int x, int y, int dist) {
    if (x < 0 || x >= n || y < 0 || y >= n || dist >= val[x][y]) return ;
    if (~dist) val[x][y] = dist;
    for (int i = 0; i < 4; ++i) {
      update(x+dx[i], y+dy[i], val[x][y]+seat[x][y]);
    }
  };

  long long ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int pos;
    cin >> pos; --pos;
    int x = pos / n, y = pos % n;
    ans += val[x][y];
    seat[x][y] = 0;
    update(x, y, -1);
  }

  cout << ans << '\n';
}
