N, = list(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()

        ccs = []

        if v>N:

            ccs.append(v-N)

        if v<N**2+1-N:

            ccs.append(v+N)

        if v%N:

            ccs.append(v+1)

        if v%N!=1:

            ccs.append(v-1)

        for u in ccs:

            if R[v]>=R[u] or ((1-V[v]) and R[v]+1 >= R[u]):

                continue

            R[u] -= 1

            stack.append(u)

print(r)

N, = list(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:

            if not(R[v]>=R[v-N] or ((1-V[v]) and R[v]+1 >= R[v-N])):

                R[v-N] -= 1

                stack.append(v-N)

        if v<N**2+1-N:

            if not(R[v]>=R[v+N] or ((1-V[v]) and R[v]+1 >= R[v+N])):

                R[v+N] -= 1

                stack.append(v+N)

        if v%N:

            if not(R[v]>=R[v+1] or ((1-V[v]) and R[v]+1 >= R[v+1])):

                R[v+1] -= 1

                stack.append(v+1)

        if v%N!=1:

            if not(R[v]>=R[v-1] or ((1-V[v]) and R[v]+1 >= R[v-1])):

                R[v-1] -= 1

                stack.append(v-1)

print(r)

(N,) = list(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()
        ccs = []
        if v > N:
            ccs.append(v - N)
        if v < N**2 + 1 - N:
            ccs.append(v + N)
        if v % N:
            ccs.append(v + 1)
        if v % N != 1:
            ccs.append(v - 1)
        for u in ccs:
            if R[v] >= R[u] or ((1 - V[v]) and R[v] + 1 >= R[u]):
                continue
            R[u] -= 1
            stack.append(u)
print(r)

(N,) = list(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:
            if not (R[v] >= R[v - N] or ((1 - V[v]) and R[v] + 1 >= R[v - N])):
                R[v - N] -= 1
                stack.append(v - N)
        if v < N**2 + 1 - N:
            if not (R[v] >= R[v + N] or ((1 - V[v]) and R[v] + 1 >= R[v + N])):
                R[v + N] -= 1
                stack.append(v + N)
        if v % N:
            if not (R[v] >= R[v + 1] or ((1 - V[v]) and R[v] + 1 >= R[v + 1])):
                R[v + 1] -= 1
                stack.append(v + 1)
        if v % N != 1:
            if not (R[v] >= R[v - 1] or ((1 - V[v]) and R[v] + 1 >= R[v - 1])):
                R[v - 1] -= 1
                stack.append(v - 1)
print(r)

-        ccs = [] -            ccs.append(v - N) +            if not (R[v] >= R[v - N] or ((1 - V[v]) and R[v] + 1 >= R[v - N])): +                R[v - N] -= 1 +                stack.append(v - N) -            ccs.append(v + N) +            if not (R[v] >= R[v + N] or ((1 - V[v]) and R[v] + 1 >= R[v + N])): +                R[v + N] -= 1 +                stack.append(v + N) -            ccs.append(v + 1) +            if not (R[v] >= R[v + 1] or ((1 - V[v]) and R[v] + 1 >= R[v + 1])): +                R[v + 1] -= 1 +                stack.append(v + 1) -            ccs.append(v - 1) -        for u in ccs: -            if R[v] >= R[u] or ((1 - V[v]) and R[v] + 1 >= R[u]): -                continue -            R[u] -= 1 -            stack.append(u) +            if not (R[v] >= R[v - 1] or ((1 - V[v]) and R[v] + 1 >= R[v - 1])): +                R[v - 1] -= 1 +                stack.append(v - 1)