fun main(args: Array<String>) {
    val inLine1 = readLine()?.split(" ").orEmpty()
    val inLine2 = readLine()?.split(" ").orEmpty()
    val numK = inLine1[0].toInt()
    val numN = inLine1[1].toInt()
    val houseList = mutableListOf<Int>()
    val pathList = mutableListOf<Int>()


    for(i in inLine2.indices){
        houseList.add(inLine2[i].toInt())
    }
    var i = 0
    while(i < numN) {
        if (i < numN - 1){
            pathList.add(houseList[i + 1] - houseList[i])
        }else{
            pathList.add((numK - houseList[i]) + houseList[0])
        }
        i += 1
    }

    val result = numK - pathList.max()!!

    println(result)
}