import numpy as np


def resolve(N, Ps):
    seats = np.arange(N**2, dtype=np.int32)
    seats_sq = seats.reshape((N,N))

    hate_count = 0
    def n2ij(n):
        i = n // N
        j = n % N
        return (i,j)

    for p in Ps:
        seats[p-1] = -1
        i, j = n2ij(p-1)

        up = seats_sq[0:i, j]
        up = up[up > -1]
        down = seats_sq[i+1:, j]
        down = down[down > -1]
        left = seats_sq[i, 0:j]
        left = left[left > -1]
        right = seats_sq[i, j+1:]
        right = right[right > -1]

        hate_count += min([up, down, left, right], key=lambda houkou: houkou.size).size
    
    return hate_count

def main():
    N = int(input())
    Ps = list(map(int, input().split()))

    ans = resolve(N, Ps)

    print(ans)

def test():
    N = 3
    Ps = list(map(int, "1 3 7 9 5 4 8 6 2".split()))
    expected = 1
    ans = resolve(N, Ps)
    if ans == expected:
        print("OK") 
    else:
        print(f"ans={ans} but expected={expected}")

    N = 4
    Ps = list(map(int, "6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8".split()))
    expected = 3
    ans = resolve(N, Ps)
    if ans == expected:
        print("OK") 
    else:
        print(f"ans={ans} but expected={expected}")

    N = 6
    Ps = list(map(int, "11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30".split()))
    expected = 11
    ans = resolve(N, Ps)
    if ans == expected:
        print("OK") 
    else:
        print(f"ans={ans} but expected={expected}")

if __name__ == '__main__':
    #test()
    main()