import java.io.{BufferedOutputStream, BufferedReader, InputStreamReader, PrintWriter}

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedOutputStream(System.out))
    val Array(k, n) = in.readLine().split("\\s+").map(_.toInt)
    val as = in.readLine().split("\\s+").map(_.toInt)

    val distances = Array.ofDim[Int](as.length)
    for(i <- 0 until as.length - 1) {
      distances(i) = as(i + 1) - as(i)
    }
    distances(distances.length - 1) = k + as(0) - as.last

    out.println(k - distances.max)
    out.flush()
  }
}
