# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    P=tuple(map(int1,input().split()))
    d=[[0]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            d[i][j]=min(min(i,N-1-i),min(j,N-1-j))
    used=[[1]*N for _ in range(N)]
    dy,dx=[1,0,-1,0],[0,1,0,-1]
    ans=0
    for p in P:
        y,x=p//N,p%N
        ans+=d[y][x]
        used[y][x]=0
        que=deque([(y,x)])
        while que:
            y,x=que.popleft()
            for k in range(4):
                ny,nx=y+dy[k],x+dx[k]
                if used[y][x]==0:
                    if 0<=ny<N and 0<=nx<N and d[ny][nx]-d[y][x]>=1:
                        d[ny][nx]-=1
                        que.append((ny,nx))
                else:
                    if 0<=ny<N and 0<=nx<N and d[ny][nx]-d[y][x]>=2:
                        d[ny][nx]-=1
                        que.append((ny,nx))
    print(ans)

if __name__ == '__main__':
    main()
