#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

int dx[] = {1,0,0,-1};
int dy[] = {0,1,-1,0};
int n;
vector<vector<bool>> is(550,vector<bool>(550,1));
void cha(int y,int x,vector<vector<ll>> &v,int k,ll &c){
  rep(j,4){
    int ny = y+dy[j];
    int nx = x+dx[j];
    if(ny < 0 || ny >= n || nx < 0 || nx >= n)continue;
    if(v[ny][nx] > c+k){
      v[ny][nx] = c+k;
      cha(ny,nx,v,k+is[ny][nx],c);
    }
  }
}

int main(){

  cin >> n;
  //vector<string> v(n,string(n,'0'));
  vector<vector<ll>> v(n,vector<ll>(n,0));
  rep(i,n)rep(j,n)v[i][j] += min({i,j,n-i-1,n-j-1});
  int suu = n*n;
  ll res = 0;
  rep(i,suu){
    int p;scanf("%d",&p);
    int y = p/n,x = p%n-1;
    if((p%n) == 0)y--,x += n;
    res += v[y][x];
    is[y][x] = false;
    cha(y,x,v,0,v[y][x]);
  }
  cout << res << endl;
  




  return 0;
}