import numpy as np
from libc.string cimport memset, strlen
from libc.stdio cimport scanf, printf
from libc.stdlib cimport malloc, free
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

ctypedef long long LL
ctypedef unsigned int uint
ctypedef unsigned long ulong
ctypedef unsigned long long uLL
ctypedef (int ,int) pair

cdef LL mod = 10**9+7
cdef LL inf = 10**18
cdef int[4] dx = [0, 1, 0, -1], dy = [1, 0, -1, 0]



cdef int solve(int N, int[:] P):
    cdef:
        int ans
        int[505][505] path, stay
    
    cdef int i, j
    for i in range(N):
        for j in range(N):
            path[i][j] = min(i+1, j+1, N-i, N-j) - 1
            stay[i][j] = 1

    ans = 0
    cdef int x, y, nx, ny, pos
    cdef pair[500*500] node
    for i in range(N*N):
        x = P[i]//N
        y = P[i]%N
        ans += path[x][y]
        stay[x][y] = 0
        
        pos = 0
        node[pos] = (x, y)
        while pos >= 0:
            x, y = node[pos]
            pos -= 1
            for j in range(4):
                nx = x + dx[j]
                ny = y + dy[j]
                if nx < 0 or nx >= N or ny < 0 or ny >= N:
                    continue
                if path[nx][ny] > path[x][y] + stay[x][y]:
                    path[nx][ny] = path[x][y] + stay[x][y]
                    pos += 1
                    node[pos] = (nx, ny)
    print(ans)
    return 0


def main():
    N = int(input())
    P = np.array(input().split(), dtype=np.intc)
    solve(N, P)


if __name__ == "__main__":
    main()
