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 cycleList = (0 until columnCount).map { 0 } var numList = (0 until columnCount).map { it } for(i in (1..100)) { var tmp = numList.withIndex().sortedBy { moveTo[it.index] }.map { it.value } numList = tmp cycleList = cycleList.withIndex().map { if(it.value > 0) it.value else(if (numList[it.index] == it.index) i else 0) } } val ans = cycleList.map { it.toLong() }.reduce { acc, i -> getMinKoubai(acc, i) } println(ans) } fun getMinKoubai(num1:Long, num2:Long):Long { val kouyaku = getMaxKouyaku(num1, num2) return num1 * num2 / kouyaku } fun getMaxKouyaku(num1:Long, num2:Long):Long { if(num1 % num2 == 0L) { return num2 } return getMaxKouyaku(num2, num1%num2) } 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 }