

import java.lang.Math.abs

fun main(args:Array<String>) {
    val (n,k) = readLine()!!.split(" ").map { it.toInt() }
    val  arr = readLine()!!.split(" ").map { it.toInt() }
    var ans = Int.MAX_VALUE
    for(i in 0 until k) {
        val l = (k + i - 1) % k
        val r = (i + 1) % k
        var ld =  if(l > i) n - (arr[l] - arr[i]) else arr[i] - arr[l]
        var rd = if(r < i) n - (arr[i] - arr[r]) else arr[r] - arr[i]
        ld = n - ld
        rd = n - rd
        val d = if(ld > rd) rd else ld
        if(ans > d) {
            ans = d
        }
    }
    print(ans)
}