Problem:
Score : 600 points

Problem Statement

Given is an integer N.
Find the minimum possible positive integer k such that (1+2+\cdots+k) is a multiple of N.
It can be proved that such a positive integer k always exists.

Constraints

1 \leq N \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer in a line.

Sample Input 1

11

Sample Output 1

10

1+2+\cdots+10=55 holds and 55 is indeed a multple of N=11.
There are no positive integers k \leq 9 that satisfy the condition, so the answer is k = 10.

Sample Input 2

20200920

Sample Output 2

1100144

Solution in Kotlin (1.3.71) (judge verdict: Wrong Answer)

import java.math.BigInteger
import kotlin.math.min

fun main() {
    val n = readLine()!!.toLong()
    if (n == 1L) {
        println(1L)
        return
    }
    var f = 1L
    var answer = if (n % 2L == 1L) n else (2L * n)
    val factorSet = mutableSetOf<Long>()
    while (f * f <= n) {
        if (n % f == 0L) {
            factorSet.add(f)
            factorSet.add(n / f)
        }
        f++
    }
    val factors = factorSet.toList().sorted()
    fun totient(k: Long): Long {
        var k = k
        var res = 1L
        for (p in factors) {
            if (p != 1L && k % p == 0L) {
                res *= p - 1L
                k /= p
                while (k % p == 0L) {
                    res *= p
                    k /= p
                }
            }
        }
        return res
    }
    fun check(g: Long, h: Long) {
        //println("g = $g, h = $h")
        answer = min(answer, g * g.pow(totient(h) - 1L, h))
        //println(answer)
        answer = min(answer, h * h.pow(totient(g) - 1L, g))
        //println(answer)
    }
    for (f in factors) {
        if (f != 1L && f * f <= n && gcd(f, n / f) == 1L) {
            when {
                f % 2L == 0L -> check(2L * f, n / f)
                (n / f) % 2L == 0L -> check(f, 2L * (n / f))
                else -> check(f, n / f)
            }
        }
    }
    println(answer - 1L)
}

fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b)

fun Long.pow(power: Long, mod: Long): Long {
    val mod = BigInteger.valueOf(mod)
    var e = power
    var b = BigInteger.valueOf(this) % mod
    var res = BigInteger.ONE
    while (e > 0) {
        if (e and 1L != 0L) {
            res *= b
            res %= mod
        }
        b *= b
        b %= mod
        e = e shr 1
    }
    return res.longValueExact()
}