import sys
input = sys.stdin.readline
from numba import njit
import numpy


def read():
    N = int(input().strip())
    line = input().strip()
    P = numpy.fromstring(line, dtype=numpy.int32, sep=" ")
    P -= 1
    P.reshape((N, N))
    return N, P


@njit
def dfs(N, Q, visited, y, x, D):
    for dy, dx in D:
        ny, nx = y + dy, x + dx
        if 0 <= ny < N and 0 <= nx < N:
            # 4隣接が除去済みマスなら、コストなしで到達可能
            if not visited[ny, nx] and Q[ny, nx, 0] > Q[y, x, 0] + Q[ny, nx, 1]:
                Q[ny, nx, 0] = Q[y, x, 0] + Q[ny, nx, 1]
                visited[ny, nx] = True
                dfs(N, Q, visited, ny, nx, D)


@njit
def np_solve(N, P, INF=10**9):
    Q = numpy.empty((N, N, 2), dtype=numpy.int32)
    
    for i in range(N):
        for j in range(N):
            Q[i, j, :] = min([i, j, N-i-1, N-j-1]) + 1, 1

    D = ((0, 1), (1, 0), (-1, 0), (0, -1))
    visited = numpy.zeros((N, N), dtype=numpy.bool_)
    ans = 0

    for p in P:
        y, x = p//N, p%N
        r = INF
        for dy, dx in D:
            ny, nx = y + dy, x + dx
            if 0 <= ny < N and 0 <= nx < N:
                r = min(r, Q[ny, nx, 0])
            else:
                r = 0
        ans += r
        Q[y, x, :] = r, 0
        visited.fill(False)
        visited[y, x] = True
        dfs(N, Q, visited, y, x, D)
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = np_solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))
