object Main extends App{
  import scala.io.StdIn._
  val Array(k,n)=readLine().split(" ").map(_.toInt)
  var f=0;var l=new Array[Int](n);var b=0;
  val a=readLine().split(" ").map(_.toInt)
  for{i <- (0 until n)}{l(i)=a(i)-f;f=a(i)}
  l(0)+=k-a(n-1)
  for {i <- (0 until n)}{b=Math.max(l(i),b)}
  println(l.sum-b)
}