n = gets.to_i
m = n * n
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)

dfs =->(pos, s) {
    return if a[pos] <= s
    a[pos] = s
    s += b[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 
}

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
