import java.util.Scanner

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

  val x = for (i <- 0 until N) yield {
    val j = (N + i - 1) % N
    math.abs(A(i) - (if (j < i) A(j) + K else A(j)))
  }

  println(x.min)
}
