#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int a[maxn][maxn],val[maxn][maxn],N;
void update(int x,int y)
{
  if(x == 0 || y == 0 || x == N+1 || y == N+1) return;
  int tmp = min(min(val[x+1][y],val[x-1][y]), min(val[x][y-1],val[x][y+1])) + a[x][y];
  if(tmp != val[x][y])
  {
    val[x][y] = tmp;
    update(x+1,y);
    update(x-1,y);
    update(x,y+1);
    update(x,y-1);
  }
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  for(int i = 1; i <= N; i++)
  for(int j = 1; j <= N; j++)
  {
    a[i][j] = 1;
    val[i][j] = min(min(i,N-i+1),min(j,N-j+1));
  }
  int ans = 0;
  for(int i = 0; i < N*N; i++)
  {
    int data;
    cin >> data;
    data += N-1;
    int x = data / N, y = data%N + 1;
    ans += val[x][y]-1;
    a[x][y] = 0;
    update(x,y);
  }
  cout << ans << endl;
}
