sum = 0
n = gets.to_i
BAUM = n.times.map { sum += gets.to_i }
LENGTH = BAUM.last
def cut from, to
  p = BAUM[to - 1] - BAUM[from - 1]
  p < 0 ? p + LENGTH : p
end

limit = LENGTH / 3
b_fix = c_fix =  a_b = b_c = nil
(1...n - 1).each {|b|
  b_fix = b
  a_b = cut 0, b
  break if a_b > limit
}
remain_limit = limit / 2
b_c = nil
(b_fix + 1..n).each {|c|
  c_fix = c
  b_c = cut b_fix, c
  break if b_c > remain_limit
}
least = [a_b, b_c, cut(c_fix, 0)].min

(0...n - 2).each {|a|
  (a + 1...n - 1).each {|b|
    a_b = cut a, b
    next if a_b <= least
    break if (LENGTH - a_b) < least * 2
    (b + 1...n).each {|c|
      b_c = cut b, c
      next if b_c <= least
      c_a = cut c, a
      break if c_a < least
      least = [[a_b, b_c, c_a].min, least].max
    }
  }
}
puts least