import java.util.*

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)
    var ar = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()
    var k=ar[0]
    var n=ar[1]

    var a = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()
    var min = Int.MAX_VALUE
    var temp = 0

    for(i in 0..a.size-1)
    {
        if(i+1<=a.size-1)
           temp= a[i]+k-a[i+1]
        if(temp<min)
            min=temp
    }

    temp = a[a.size-1]-a[0]
    if(temp<min)
        min=temp

    println(min)
}
