object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val k, n = sc.nextInt()
  val as = List.fill(n)(sc.nextInt())
  val diff = (as.tail :+ as.head + k).zip(as).map { case (a, b) => a - b }
  println(k - diff.max)
}
