#!/usr/bin/env python3
from collections import deque

N=int(input())
b=[[0]*N for _ in range(N)]
c=[[1]*N for _ in range(N)]

q = deque()

for i in range(N):
    for j in range(N):
        b[i][j]=min(i,j, N-i-1, N-j-1)

def decb(q, hito):
    x = hito % N
    y = hito // N

    v=9999
    if y>0: v=b[y-1][x]+c[y-1][x]
    else: v=0
    if y<N-1: v=min(v, b[y+1][x]+c[y+1][x])
    else: v=0
    if x>0: v=min(v, b[y][x-1]+c[y][x-1])
    else: v=0
    if x<N-1: v=min(v, b[y][x+1]+c[y][x+1])
    else: v=0

    b[y][x]=v

    if x>0 and b[y][x-1] > v: q.append(hito-1)
    if x<N-1 and b[y][x+1] > v: q.append(hito+1)
    if y>0 and b[y-1][x] > v: q.append(hito-N)
    if y<N-1 and b[y+1][x] > v: q.append(hito+N)

def unsetb(hito):
    global N, b
    x = hito % N
    y = hito // N
    c[y][x]=0
    ret = b[y][x]
    q.append(hito)

    while len(q)>0:
        decb(q, q.pop())
    return ret

def main():
    global b
    A=list(map(int, input().split()))
    ans=0
    for hito in A:
        ans+=unsetb(hito-1)
    print(ans)

if __name__ == '__main__':
    main()
