[n], *e = [[*map(int, t.split())] for t in open(0)]
viewers = set(range(1, n**2 + 1))
ans = 0

for x in e[0]:
    visited = set()
    clear = {x for x in range(1, n**2 + 1) if x %
             n == 0 or x % n == 1 or x <= n or x > n**2 - n}

    def solve(x):
        if x not in clear:
            visited.add(x)
            tmp = n
            for k in [x - 1, x + 1, x - n, x + n]:
                if tmp == 0:
                    clear.add(x)
                    break
                else:
                    if k not in visited:
                        tmp = min(tmp, (k in viewers) + solve(k))
            visited.remove(x)
            return tmp
        return 0

    ans += solve(x)
    viewers.remove(x)

print(ans)
