class UnionFind:
    __slots__ = ["_size", "_first_idx", "_parents", "_cost"]

    def __init__(self, size: int, N: int, first_index: int = 0) -> None:
        self._size = size
        self._first_idx = first_index
        self._parents = [-1] * (size + first_index)

        cost = [0] * (N * N)
        for r in range(N):
            dr = min(r, N - 1 - r)
            for c in range(N):
                dc = min(c, N - 1 - c)
                cost[r * N + c] = min(dr, dc)
        self._cost = cost

    def find(self, x: int) -> int:
        """Find the group (root) of vertex x"""
        if self._parents[x] < 0:
            return x
        self._parents[x] = self.find(self._parents[x])
        return self._parents[x]

    def is_same(self, x: int, y: int) -> bool:
        """Return whether two vertices x and y are in the same group or not."""
        return self.find(x) == self.find(y)

    def unite(self, x: int, y: int) -> None:
        """Unite two groups of vertices x and y."""
        c = min(self._cost[x], self._cost[y])
        x, y = self.find(x), self.find(y)
        if x == y:
            self._cost[x] = min(self._cost[x], c)
            return

        if self._parents[x] > self._parents[y]:
            x, y = y, x
        self._parents[x] += self._parents[y]
        self._parents[y] = x
        self._cost[x] = min(self._cost[x], self._cost[y], c)

    def get_size(self, x: int) -> int:
        """Get the size of the group vertex x belongs"""
        return -self._parents[self.find(x)]

    def get_cost(self, x: int) -> int:
        x = self.find(x)
        return self._cost[x]

    def set_cost(self, x: int, val: int):
        x = self.find(x)
        self._cost[x] = min(self._cost[x], val)


def main():
    N = int(input())
    *p, = (int(x) - 1 for x in input().split())

    g = [[set() for _ in range(N)] for _ in range(N)]
    for r in range(N):
        for c in range(N):
            if r > 0:
                g[r][c].add((r - 1, c))
            if c > 0:
                g[r][c].add((r, c - 1))
            if r < N - 1:
                g[r][c].add((r + 1, c))
            if c < N - 1:
                g[r][c].add((r, c + 1))

    uf = UnionFind(N * N, N)

    ex = [[0] * N for _ in range(N)]

    ans = 0
    for pi in p:
        r, c = divmod(pi, N)

        t = uf.get_cost(pi)
        for nr, nc in g[r][c]:
            if uf.is_same(nr * N + nc, pi):
                continue
            else:
                t = min(t, uf.get_cost(nr * N + nc) + (0 if ex[nr][nc] else 1))
        ans += t

        ex[r][c] = 1
        for nr, nc in g[r][c]:
            if ex[nr][nc]:
                uf.unite(nr * N + nc, pi)

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
#
# sys.setrecursionlimit(10 ** 7)
#
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok
