@n = gets.to_i
@m = @n ** 2
pp = gets.split.map(&:to_i)

@a = Array.new(@m) do |i|
    x, y = i.divmod(@n)
    x = @n - x - 1 if x > @n - x - 1
    y = @n - y - 1 if y > @n - y - 1
    x < y ? x : y
end
@b = Array.new(@m, 1)

def dfs(pos, s)
    return if @a[pos] <= s
    @a[pos] = s
    s += @b[pos]


    if pos > @n - 1
        dfs(pos - @n, s) 
    end
    if pos > 0
        dfs(pos -  1, s)
    end
    if pos < @m - 1
        dfs(pos +  1, s)
    end
    if pos < @m - @n 
        dfs(pos + @n, s) 
    end
end

ans = 0
pp.each do |pos|
    pos -= 1
    ans += @a[pos]
    @b[pos] = 0
    s = @a[pos]
    dfs(pos - @n, s) if pos > @n - 1
    dfs(pos -  1, s) if pos > 0
    dfs(pos +  1, s) if pos < @m - 1
    dfs(pos + @n, s) if pos < @m - @n 
end

p ans
