def check(left, cur, n):
    x, y = cur // n, cur % n
    l, r = x * n, x * n + (n-1)
    top, bot = y, n * (n-1) + y
    res = n
    foo = lambda x: x not in left
    res = min(res, len(list(filter(foo, range(l, cur)))))
    res = min(res, len(list(filter(foo, range(cur+1, r+1)))))
    res = min(res, len(list(filter(foo, range(top, cur, n)))))
    res = min(res, len(list(filter(foo, range(cur+1, bot, n)))))
    # print(cur, res)
    return res

N = int(input())
P = list(map(int, input().split()))
left = set()
ans = 0
for p in P:
    ans += check(left, p-1, N)
    left.add(p-1)
print(ans)