import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val K = sc.nextInt
  val N = sc.nextInt
  val A = List.fill(N)(sc.nextInt)

  val x = (0 until N).map(i => {
    val As = A.splitAt(i)
    val x = As._2 ++ As._1.map(_ + K)
    math.abs(x.head - x.last)
  })

  println(x.min)
}
