Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = h.pop()
        if not dp2[y][x]:
            d += 1
        for j in range(4):
            ny, nx = y + dy[j], x + dx[j]
            if 0 <= ny < n and 0 <= nx < n:
                if dp[ny][nx] > d:
                    dp[ny][nx] = d
                    h.append((d, ny, nx))
print(ans)
    
    


Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = h.pop()
        if not dp2[y][x]:
            d += 1
        if x != 0:
            if dp[y][x-1] > d:
                dp[y][x-1] = d
                h.append((d, y, x-1))
        if x != n-1:
            if dp[y][x+1] > d:
                dp[y][x+1] = d
                h.append((d, y, x+1))
        if y != 0:
            if dp[y-1][x] > d:
                dp[y-1][x] = d
                h.append((d, y-1, x))
        if y != n-1:
            if dp[y+1][x] > d:
                dp[y+1][x] = d
                h.append((d, y+1, x))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = heappop(h)
        if not dp2[y][x]:
            d += 1
        if x != 0:
            if dp[y][x-1] > d:
                dp[y][x-1] = d
                heappush(h, (d, y, x-1))
        if x != n-1:
            if dp[y][x+1] > d:
                dp[y][x+1] = d
                heappush(h, (d, y, x+1))
        if y != 0:
            if dp[y-1][x] > d:
                dp[y-1][x] = d
                heappush(h, (d, y-1, x))
        if y != n-1:
            if dp[y+1][x] > d:
                dp[y+1][x] = d
                heappush(h, (d, y+1, x))
print(ans)
    
    

Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = h.pop()
        if not dp2[y][x]:
            d += 1
        if x != 0:
            if dp[y][x-1] > d:
                dp[y][x-1] = d
                h.append((d, y, x-1))
        if x != n-1:
            if dp[y][x+1] > d:
                dp[y][x+1] = d
                h.append((d, y, x+1))
        if y != 0:
            if dp[y-1][x] > d:
                dp[y-1][x] = d
                h.append((d, y-1, x))
        if y != n-1:
            if dp[y+1][x] > d:
                dp[y+1][x] = d
                h.append((d, y+1, x))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = heappop(h)
        if not dp2[y][x]:
            d += 1
        for j in range(4):
            ny, nx = y + dy[j], x + dx[j]
            if 0 <= ny < n and 0 <= nx < n:
                if dp[ny][nx] > d:
                    dp[ny][nx] = d
                    heappush(h, (d, ny, nx))
print(ans)
    
    

Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = h.pop()
        if not dp2[y][x]:
            d += 1
        if x != 0:
            if dp[y][x-1] > d:
                dp[y][x-1] = d
                h.append((d, y, x-1))
        if x != n-1:
            if dp[y][x+1] > d:
                dp[y][x+1] = d
                h.append((d, y, x+1))
        if y != 0:
            if dp[y-1][x] > d:
                dp[y-1][x] = d
                h.append((d, y-1, x))
        if y != n-1:
            if dp[y+1][x] > d:
                dp[y+1][x] = d
                h.append((d, y+1, x))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()

ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
for i in p:
    q = (i-1) % n
    r = (i-1) // n
    ans += dp[r][q]
    dp2[r][q] = True
    h = []
    heappush(h, (dp[r][q], r, q))
    while h:
        d, y, x = heappop(h)
        if not dp2[y][x]:
            d += 1
        for j in range(4):
            ny, nx = y + dy[j], x + dx[j]
            if 0 <= ny < n and 0 <= nx < n:
                if dp[ny][nx] > d:
                    dp[ny][nx] = d
                    heappush(h, (d, ny, nx))
print(ans)
    
    

Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
p = [((i-1)%n, (i-1)//n) for i in p]
ans = 0
dp = [[min(i, j, n - (i + 1), n - (j + 1)) for j in range(n)] for i in range(n)]
dp2 = [[False]*n for _ in range(n)]
for (q, r) in p:
    ans += dp[r][q]
    dp2[r][q] = True
    h = [(dp[r][q], r, q)]
    while h:
        d, y, x = h.pop()
        if not dp2[y][x]:
            d += 1
        if x != 0:
            if dp[y][x-1] > d:
                dp[y][x-1] = d
                h.append((d, y, x-1))
        if x != n-1:
            if dp[y][x+1] > d:
                dp[y][x+1] = d
                h.append((d, y, x+1))
        if y != 0:
            if dp[y-1][x] > d:
                dp[y-1][x] = d
                h.append((d, y-1, x))
        if y != n-1:
            if dp[y+1][x] > d:
                dp[y+1][x] = d
                h.append((d, y+1, x))
print(ans)