import heapq
push = heapq.heappush
pop = heapq.heappop

def main():

    N = int(input())
    S = list(map(int, input().split()))
    NN = N**2
    P = [True]*(NN)

    def bfs(loc):
        data = [True]*(NN)
        data[loc] = 1
        s = P[loc]
        stack = [(0, loc)]
        while True:
            d, v = pop(stack)
            # 上
            if v-N >= 0:
                if data[v-N]:
                    data[v-N] = False
                    if P[v-N]:
                        push(stack, (d+1, v-N))
                    else:
                        push(stack, (d, v-N))
            else:
                return d
            # 左
            if v%N != 0:
                if data[v-1]:
                    data[v-1] = False
                    if P[v-1]:
                        push(stack, (d+1, v-1))
                    else:
                        push(stack, (d, v-1))
            else:
                return d
            # 右
            if (v+1)%N != 0 and v != NN-1:
                if data[v+1]:
                    data[v+1] = False
                    if P[v+1]:
                        push(stack, (d+1, v+1))
                    else:
                        push(stack, (d, v+1))
            else:
                return d
            # 下
            if v+N < NN:
                if data[v+N]:
                    data[v+N] = False
                    if P[v+N]:
                        push(stack, (d+1, v+N))
                    else:
                        push(stack, (d, v+N))
            else:
                return d

    ans = 0
    for i in range(NN):
        P[S[i]-1] = False
        ans += bfs(S[i]-1)
    print(ans)

if __name__ == "__main__":
    main()