S = {}

def dsMakeSet(ds, x):
    ds[x] = x


def dsFind(ds, x):
    if ds[x] == x:
        return x

    x0 = x
    while ds[x] != x:
        x = ds[x]

    ds[x0] = x
    return x


def dsUnion(ds, x, y):
    xRoot = dsFind(ds, x)
    yRoot = dsFind(ds, y)
    
    if xRoot != yRoot:
        ds[yRoot] = xRoot
        S[xRoot] = tuple([min(xy[0], xy[1]) for xy in zip(S[xRoot], S[yRoot])])
        return True

    return False



N = int(input())
P = list(map(int, input().split()))
ds = {}
ans = 0

for p in P:
    row = (p-1)//N
    col = (p-1)%N
    key = (row, col)

    if not(key in ds):
        dsMakeSet(ds, key)
        S[key] = (row, col, N-row-1, N-col-1)
        
        if (row-1, col) in ds:
            dsUnion(ds, key, (row-1, col))
            
        if (row+1, col) in ds:
            dsUnion(ds, key, (row+1, col))
            
        if (row, col-1) in ds:
            dsUnion(ds, key, (row, col-1))
            
        if (row, col+1) in ds:
            dsUnion(ds, key, (row, col+1))
            
        root = dsFind(ds, key)
        ans += min(S[root])

print(ans)

    
