fun main(args: Array<String>) {
    var (k, n)=readLine()!!.split(' ').map { it.toInt() }
    var a=readLine()!!.split(' ').map { it.toInt() }

    var max=0
    for(i in 0 until n){
        if (i!=n-1){
            if(a[i+1]-a[i]>max){
                max=a[i+1]-a[i]
            }
        }else{
            if(k-a[i]+a[0]>max){
                max=k-a[i]+a[0]
            }
        }
    }
    println(k-max)
}