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

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