#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

int main()
{
  int n;
  cin>>n;
  vector<vector<bool>> grid(n,vector<bool>(n,true));
  vector<vector<int>> dist(n,vector<int>(n,0));
  rep(i,n)rep(j,n) dist[i][j]=min(i,min(j,min(n-i-1,n-j-1)));
  //rep(i,n){rep(j,n) cout<<dist[i][j]; cout<<"\n";}
  
  vector<int> dy = {0,1,0,-1}, dx = {1,0,-1,0};
  int res = 0;
  
  rep(i,n*n)
  {
    int p;
    cin>>p;
    p--;
    int sy = p/n, sx = p%n;
    grid[sy][sx] = false;
    queue<P> q;
    q.push(P(sy, sx));
    
    while(q.size())
    {
      int y = q.front().first, x = q.front().second;
      q.pop();
      int nd = dist[y][x];
      if(grid[y][x]) nd++;
      
      rep(j,4)
      {
        int ny = y+dy[j], nx = x+dx[j];
        if(ny<0||ny>=n||nx<0||nx>=n) continue;
        if(dist[ny][nx] > nd)
        {
          dist[ny][nx] = nd;
          q.push(P(ny, nx));
        }
      }
    }
    res += dist[sy][sx];
    
  }
  cout << res << endl;
  //rep(i,n){rep(j,n) cout<<dist[i][j]; cout<<"\n";}
  
  return 0;
}