object Main extends App{
  val k_n = io.StdIn.readLine().split(' ').map(_.toInt)
  val k = k_n(0)
  val n = k_n(1)
  val An = io.StdIn.readLine().split(' ').map(_.toInt)
  val candidate = for(i <- 0 until n) yield (k + An((i+n-1)%n) - An(i)) % k
  println(candidate.min)
}
