
fun main(args: Array<String>) {
    val (K, N) = readLine()!!.split(" ").map(String::toInt)
    val An = readLine()!!.split(" ").map(String::toInt)

    val diff_list = (0 until (N - 1)).map {An[it + 1] - An[it]}.toMutableList()
    diff_list.add(K + (An[0] - An[N - 1]))
    val diff = diff_list.max()!!
    (K - diff).run(::println)
}