fun main(args: Array<String>) {
    val s = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toInt() }
    val x = s[1] - 1
    val distanceArray = Array(s[1]){0}
    for (i in 0..x-1){
        distanceArray[i] = a[x-i] - a[x-i-1]
    }
    val n = s[0] - a[x]
    distanceArray[x] = n + a[0]
    val max = distanceArray.max()!!
    val kotae = s[0] - max
    println(kotae)
}
