#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int ijToNum(int nLine, int i, int j){
  	int seatNum = j*nLine+i+1;
  	return seatNum;
}

int numToJ(int nLine, int seatNum){
 	int jGuest = ( (seatNum-1) / nLine);// [0,N-1]
  	return jGuest;
}

int numToI(int nLine, int seatNum){
 	int yGuest = numToJ(nLine, seatNum) + 1;// [1,N]
    int xGuest = seatNum - nLine*(yGuest-1);// [1,N]
  	return xGuest-1;
}

int main(){
  	int answer =0;
  	
	int nLine;
  	cin >> nLine;
    int mvCost[nLine][nLine];
  	bool isMan[nLine][nLine];
  	for(int i=0;i<nLine;i++){
    	for(int j=0;j<nLine;j++){
          	mvCost[i][j] = std::min( {i,j,nLine-1-i,nLine-1-j} );
          	isMan[i][j] = true;
        }
    }
  	
  	int nGuest = nLine*nLine;
  	int seatNum[nGuest];
  	for(int iGuest=0;iGuest<nGuest;iGuest++){
  		cin >> seatNum[iGuest];
    }
  	
  	std::vector<int> subtrSeat;
  	for(int iGuest=0;iGuest<nGuest;iGuest++){
      	subtrSeat.push_back(seatNum[iGuest]);
      	answer += mvCost[numToI(nLine, subtrSeat[0])][numToJ(nLine, subtrSeat[0])];
      	isMan[numToI(nLine, subtrSeat[0])][numToJ(nLine, subtrSeat[0])] = false;
      	
    }
      	while( (int)subtrSeat.size() > 0){
	      	int iGuest = numToI(nLine, subtrSeat[0]);
    	  	int jGuest = numToJ(nLine, subtrSeat[0]);
          	
          	//cout << "seatNum = " << subtrSeat[0] <<endl;
          	if(0< iGuest && iGuest<nLine-1 && 0< jGuest && jGuest<nLine-1){
 	         	if(mvCost[iGuest][jGuest] < mvCost[iGuest+1][jGuest]){
    	          	subtrSeat.push_back(ijToNum(nLine,iGuest+1,jGuest));
        	    }
            }
          	if(0< iGuest && iGuest<nLine-1 && 0< jGuest && jGuest<nLine-1){
          		if(mvCost[iGuest][jGuest] < mvCost[iGuest-1][jGuest]){
              		subtrSeat.push_back(ijToNum(nLine,iGuest-1,jGuest));
    	        }
            }
          	if(0< iGuest && iGuest<nLine-1 && 0< jGuest && jGuest<nLine-1){
	          	if(mvCost[iGuest][jGuest] < mvCost[iGuest][jGuest+1]){
    	          	subtrSeat.push_back(ijToNum(nLine,iGuest,jGuest+1));
        	    }
            }
          	if(0< iGuest && iGuest<nLine-1 && 0< jGuest && jGuest<nLine-1){
            	if(mvCost[iGuest][jGuest] < mvCost[iGuest][jGuest-1]){
              		subtrSeat.push_back(ijToNum(nLine,iGuest,jGuest-1));
                }
            }
          	//cout<< "cost = "<< mvCost[iGuest][jGuest]<<endl;
          	mvCost[iGuest][jGuest]--;
      		subtrSeat.erase(subtrSeat.begin());
          	subtrSeat.shrink_to_fit();
        }
    }
  	
  	cout << answer;
}
