fun main(args: Array<String>){
    val (K, N) = readLine()!!.split(" ").map{it.toInt()}
    val A = readLine()!!.split(" ").map{it.toInt()}
    
    var ans = A[N-1] - A[0]
    for(i in 1 until N){
        ans = Math.min(ans, K - A[i] + A[i-1])
    }
    println(ans)
}