from collections import deque

def Next(): return input()
def NextInt(): return int(Next())
def NextInts(): return map(int,input().split())
def Nexts(): return map(str,input().split())
def NextIntList(): return list(map(int,input().split()))
def RowInts(n): return [input() for i in range(n)]

def main():
    n = NextInt()
    da = NextIntList()
    for i in range(n*n):
        da[i] -= 1
    co = [[min(abs(i), abs(j), abs(n-i-1), abs(n-j-1)) for i in range(n)] for j in range(n)]
    ju = [[1 for i in range(n)] for j in range(n)]
    q = deque()
    ans = 0
    for i in range(n*n):
        nx, ny = da[i]//n, da[i]%n
        ans += co[nx][ny]
        ju[nx][ny] = 0
        q.append([nx, ny])
        while len(q) != 0:
            now = q.popleft()
            x, y = now[0],now[1]
            if x > 0 and co[x][y] + ju[x][y] < co[x-1][y]:
                co[x-1][y] = co[x][y] + ju[x][y]
                q.append([x-1, y])
            if x < n-1 and co[x][y] + ju[x][y] < co[x+1][y]:
                co[x+1][y] = co[x][y] + ju[x][y]
                q.append([x+1, y])
            if y > 0 and co[x][y] + ju[x][y] < co[x][y-1]:
                co[x][y-1] = co[x][y] + ju[x][y]
                q.append([x, y-1])
            if y < n-1 and co[x][y] + ju[x][y] < co[x][y+1]:
                co[x][y+1] = co[x][y] + ju[x][y]
                q.append([x, y+1])
    print(ans)

if __name__ == "__main__":
    main()
