object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val k, n = sc.nextInt()
  val as = Vector.fill(n)(sc.nextInt())
  val min = ((as.last - k) +: as).zip(as).foldLeft(Int.MaxValue) {
    case (acc, (a, a2)) => (a + k - a2).min(acc)
  }
  println(min)
}
