import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline

m=int(input())
n=m**2
p=list(map(int,input().split()))

inf = 0
num_min =2**(n-1).bit_length()
dat1=[inf]*(2*num_min-1)
dat2=[inf]*(2*num_min-1)

#初期化
for i in range(n):
    dat1[i+num_min-1]=1
    dat2[i+num_min-1]=1
for i in range(num_min-2,-1,-1):
    dat1[i]=dat1[2*i+1]+dat1[2*i+2]
    dat2[i]=dat2[2*i+1]+dat2[2*i+2]

#親のノード(n-1)//2
#子のノード2n+1,2n+2

def segfunc(x,y):
    return x+y

def update1(i,x):
    i+=num_min-1
    dat1[i]=x
    while i>0:
        i=(i-1)//2
        dat1[i]=segfunc(dat1[2*i+1],dat1[2*i+2])

def update2(i,x):
    i+=num_min-1
    dat2[i]=x
    while i>0:
        i=(i-1)//2
        dat2[i]=segfunc(dat2[2*i+1],dat2[2*i+2])

def query1(a,b): #半開区間、[a,b)の最小値
    if b<=a:
        return inf
    res1=inf
    a += num_min-1
    b += num_min-2
    while b-a>1:
        if a&1==0:
            res1 = segfunc(res1,dat1[a])
        if b&1==1:
            res1 = segfunc(res1,dat1[b])
        a=a//2
        b=(b-1)//2
    if a==b:
        res1 = segfunc(res1,dat1[a])
    else:
        res1=segfunc(segfunc(res1,dat1[a]),dat1[b])
    return res1
def query2(a,b): #半開区間、[a,b)の最小値
    if b<=a:
        return inf
    res2=inf
    a += num_min-1
    b += num_min-2
    while b-a>1:
        if a&1==0:
            res2 = segfunc(res2,dat2[a])
        if b&1==1:
            res2 = segfunc(res2,dat2[b])
        a=a//2
        b=(b-1)//2
    if a==b:
        res2 = segfunc(res2,dat2[a])
    else:
        res2=segfunc(segfunc(res2,dat2[a]),dat2[b])
    return res2

ans=0
for k in p:
    i=k//m
    j=k%m
    l=(j-1)*m+i+1
    if j==0:
        j=m
    res1=min(query1(i*m,k-1),query1(k,(i+1)*m))
    update1(k-1,0)
    res2=min(query2((j-1)*m,l-1),query2(l,j*m))
    r=min(res1,res2)
    ans+=r
    update2(l-1,0)
print(ans)