import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)

    val (k, n) = sc.nextLine().split(" ").map { it.toInt() }
    val a = sc.nextLine().split(" ").map { it.toInt() }

    val sub = (1 until n).map { a[it] - a[it-1] }.toList() as MutableList<Int>

    sub.add(k-a[n-1]+a[0])

    val m = sub.max()

    println(sub.sum()-m!!)
}
