import numpy as np
def main():
    N = int(input())
    P = list(map(int, input().split()))

    ans = 0
    seat = np.ones([N, N], dtype=np.int)
    for p in P:
        j = (p-1) % N
        i = (p-1-j) // N
        left = sum(seat[i, :j])
        right = sum(seat[i, j+1:])
        up = sum(seat[:, j][:i])
        down = sum(seat[:, j][i+1:])
        hate = min(left, right, up, down)
        ans += hate
        seat[i][j] = 0
    print(ans)
main()