N, = map(int, input().split())
R = [0] * (N**2+1)
V = [0] * (N**2+1)
X = list(map(int, input().split()))
for ai in range(N**2):
    i, j = divmod(ai, N)
    R[ai+1] = min([i, N-i-1, j, N-j-1])
r = 0
for a in X:
    r += R[a]
    V[a] = 1
    stack=[a]
    while stack:
        v = stack.pop()
        if v>N and (R[v]<R[v-N] and (V[v] or R[v]+1<R[v-N])):
            R[v-N] -= 1
            stack.append(v-N)
        if v<N**2+1-N and (R[v]<R[v+N] and (V[v] or R[v]+1<R[v+N])):
            R[v+N] -= 1
            stack.append(v+N)
        if v%N and (R[v]<R[v+1] and (V[v] or R[v]+1<R[v+1])):
            R[v+1] -= 1
            stack.append(v+1)
        if v%N-1 and (R[v]<R[v-1] and (V[v] or R[v]+1<R[v-1])):
            R[v-1] -= 1
            stack.append(v-1)
print(r)
