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)
    (As._2 ++ As._1.map(_ + K)).sliding(2).foldLeft(Seq.empty[Int])((totalSeq, numSeq) =>
      totalSeq :+ math.abs(numSeq.head - numSeq.last)
    ).sum
  }).min

  println(x)
}
