object Main extends App {
  val input1 = readLine
  val input2 = readLine
  
  val K = input1.split(" ")(0).toInt
  val N = input1.split(" ")(1).toInt
    
  val arr1 = (input2.split(" "))
  val arr2 = arr1.map(x => x.toInt)
  
  var answer = arr2(0) + K-arr2(N-1)
  
  for(i <- 1 to N-1){
    if(arr2(i) - arr2(i-1) > answer){
      answer = arr2(i) - arr2(i-1)
    }
  }
  
  println(K-answer)
  
}