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 Rust (1.42.0) (judge verdict: Accepted)

use num::Integer;
use proconio::input;

fn main() {
    input!(n: u64);

    let m = 2 * n;

    let ans = divisors(m)
        .iter()
        .filter_map(|&a| {
            let a = a as i64;
            let b = m as i64 / a;

            let mut ext = a.extended_gcd(&b);
            if ext.gcd != 1 {
                return None;
            }
            if ext.x >= 0 {
                let dec = ext.x / b + 1;
                ext.x -= dec * b;
                ext.y += dec * a;
            }
            Some(-ext.x * a)
        })
        .min()
        .unwrap();

    println!("{}", ans);
}

fn divisors(n: u64) -> Vec<u64> {
    let mut ret = Vec::new();
    for x in 1.. {
        if x * x > n {
            break;
        }
        if n % x == 0 {
            ret.push(x);
            if x * x < n {
                ret.push(n / x)
            }
        }
    }
    ret
}