object Main extends App {
  val in = scala.io.Source.stdin.getLines.toSeq
  val ls = in.map(_.split(' ').map(_.toInt))
  val K = ls(0)(0)
  val a = ls(1)
  val A = a :+ (K + a(0))
  val max = (1 until A.size).foldLeft(0){ (a,i) =>
    val diff = A(i) - A(i-1)
    Math.max(a, diff)
  }
  val ret = K - max
  println(ret)
}
