object Main extends App {
  val sc = new java.util.Scanner(System.in) 
  val k = sc.nextInt()
  val n = sc.nextInt()
  sc.useDelimiter("\n")
  val a = sc.next().split(" ").map(_.toInt).toList
  val d1 = a.zip(a.tail).map{case (x, y) => y -x}.toList
  val clock = a(n-1) - a(n-2)
  val unclock = a(0) + k - a(n-1)
  val d = d1 :+ clock.min(unclock)
  println(d.sum - d.max)
  
}