from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        class KeyDict(dict):
            # 辞書にないときの対応
            def __missing__(self,key):
                self[key] = key
                return key
        self.parent = KeyDict()
        self.rank = defaultdict(int)
        self.weight = defaultdict(int)

    # 根を探す
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            # 経路圧縮
            # 自分自身じゃない場合は、上にさかのぼって検索(再帰的に)
            y = self.find(self.parent[x])           
            self.weight[x] += self.weight[self.parent[x]]   #圧縮時にweightを更新(和)
            self.parent[x] = y      #親の置き換え(圧縮)
            return self.parent[x]

    # 結合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        # 低い方を高い方につなげる(親のランクによる)
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    ### 重み付き
    def weighted_union(self, x, y, w):
        # print("unite",x,y,w,self.weight)
        px = self.find(x)
        py = self.find(y)
        # 低い方を高い方につなげる(親のランクによる)
        # if px == py: return 0
        if self.rank[px] < self.rank[py]:
            self.parent[px] = py
            self.weight[px] = - w - self.weight[x] + self.weight[y]
        else:
            self.parent[py] = px
            self.weight[py] =  w + self.weight[x] - self.weight[y]
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        return 0

    # 判定
    def judge(self, x, y):
        return self.find(x) == self.find(y)
n = int(input())
Ps = list(map(int, input().split()))
def hw(p):
    h = (p-1)//n
    w = (p-1)%n
    return h,w

def toP(h,w):
    return h * n + w + 1

fs = [[0]*n for i in range(n)]
for i in range(n):
    x = min(i,n-1-i)
    for j in range(n):
        y = min(j,n-1-j)
        fs[i][j] = min(x,y)

ans = 0
mvs = [(1,0),(-1,0),(0,1),(0,-1)]

uf = UnionFind(n**2)

dists = [0] * (n**2+1)
for i in range(1,n**2+1):
    h,w = hw(i)
    dists[i] = fs[h][w]
visited = [False] * (n**2+1)

for p in Ps:
    h,w = hw(p) 
    # a = dists[uf.find(p)]
    a = dists[p]
    for dh,dw in mvs:   
        if 0<=h+dh<n and 0<=w+dw<n:
            p1 = uf.find(p)
            p2 = uf.find(toP(h+dh,w+dw))
            if not visited[toP(h+dh, w+dw)]:
                x = dists[p2] + 1
            a = min(x, a)
            if p1 != p2:
                if dists[p1] <= dists[p2]:
                    dists[p2] = dists[p1]
                else:
                    dists[p1] = dists[p2]
                uf.union(p1,p2)
    ans += a
print(ans)
    
    