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

int n;
int cost[555][555];
bool a[555][555];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int solve(int y, int x){

  typedef pair < int, int > P;
  queue < P > que;

  que.push( P(y, x) );

  while(que.size()){
    int ny = que.front().first, nx = que.front().second; que.pop();

    for(int i=0;i<4;i++){
      int xx = nx + dx[i], yy = ny + dy[i];
      if(xx < 0 || yy < 0 || xx == n || yy == n) continue;

      if(cost[yy][xx] > cost[ny][nx]+a[ny][nx]){
        cost[yy][xx] = cost[ny][nx]+a[ny][nx];
        que.push( P(yy, xx) );
      }
    }
  }
  return cost[y][x];
}

int main(){

  cin >> 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));
      a[i][j] = 1;
    }
  }

  int ans = 0;
  for(int i=0;i<n*n;i++){
    int p;
    cin >> p; p--;
    int y = p/n, x = p%n;
    a[y][x] = 0;
    ans += solve(y, x);

  }

  cout << ans << endl;

}
