import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    import numpy as np
    N = int(readline())
    P = list(map(int, readline().decode().split()))
    l = []
    for i in range(1, N**2 + 1):
        l.append(P.index(i)+1)
    seat = np.array(l).reshape((N, N))
    cnt = 0
    for col in range(N):
        for row in range(N):
            num = seat[row, col]
            u = seat[:row, col]
            d = seat[row+1:, col]
            r = seat[row, col+1:]
            le = seat[row, :col]
            cnt += min(np.count_nonzero(u[u > num]),
                       np.count_nonzero(d[d > num]),
                       np.count_nonzero(r[r > num]),
                       np.count_nonzero(le[le > num]))
    print(cnt)
    return

if __name__ == '__main__':
    main()
