import java.lang.Math fun main( args : Array ) { val (n, m) = readLine()!!.split(" ").map { it.toInt() } var ps = (1..m).map { readLine()!!.toInt() }.toList() var status = 0 var miles = 0 (0..n-1).map { val f = ps.map { x -> Pair(x, Math.abs(x - status) ) }.sortedBy { x-> x.second }.first() miles += f.second status = f.first ps = ps.filter { x -> x != status } } println( miles ) }