import kotlin.math.max fun main(arr:Array) { val columnCount = readLine()!!.toInt() val barCount = readLine()!!.toInt() val barMap = (0 until barCount).map { readLine()!!.split(" ").map { it.toInt() - 1 } } val moveTo = (0 until columnCount).map { getGoal(it, barMap) } var numList = (0 until columnCount).map { it } for(i in (1..10000000)) { var tmp = numList.withIndex().sortedBy { moveTo[it.index] }.map { it.value } numList = tmp if(numList.withIndex().all { it.index == it.value }) { println(i) return } } } fun getGoal(column:Int, barMap:List>):Int { var current = column for((f,t) in barMap) { if(f == current) { current = t } else if(current == t) { current = f } } return current }