#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(){
  int N; cin >> N;
  vector<vector<int>> seat(N, vector<int>(N));
  vector<vector<bool>> on(N, vector<bool>(N, true));
  vector<pair<int, int>> P(N * N);
  rep(a, N * N) {
    int x; cin >> x; x--;
    P[a] = make_pair(x / N, x % N);
  }

  rep(a, N) rep(b, N) {
    seat[a][b] = min({a, N - 1 - a, b, N - 1 - b});
  }

  long long ans = 0;
  rep(i, N * N) {
    int a, b;
    tie(a, b) = P[i];
    ans += seat[a][b];
    on[a][b] = false;
    queue<pair<int, int>> q;
    q.emplace(a, b);
    while(!q.empty()) {
      int x, y;
      tie(x, y) = q.front(); q.pop();
      int minnext = seat[x][y] + 1;
      if (!on[x][y]) minnext--;

      rep(d, 4) {
        int nx = x + dx[d], ny = y + dy[d];
        if (min(nx, ny) < 0 || max(nx, ny) >= N) continue;
        if (seat[nx][ny] > minnext) {
          seat[nx][ny] = minnext;
          q.emplace(nx, ny);
        }
      }
    }
  }

  cout << ans << endl;
}
