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 std::cmp::min;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n = sc.read();
    println!("{}", solve(n));
}

fn solve(n: i128) -> i128 {
    let mut c = vec![];
    for i in 1.. {
        if i * i > (2 * n) {
            break;
        }
        if (2 * n) % i == 0 {
            let j = (2 * n) / i;
            c.push((i, j));
        }
    }

    let mut ans = 2 * n;
    for (x, y) in c {
        if y > 1 {
            let m = vec![x, y];
            let b = vec![0, y - 1];
            if let Some((k, _)) = chinese_remainder_theorem(&b, &m) {
                assert_eq!(k % x, 0);
                assert_eq!(k % y, y - 1);
                assert_eq!((k * (k + 1) / 2) % n, 0);
                assert!(k > 0);
                ans = min(ans, k);
            }
        }
        let (y, x) = (x, y);
        if y > 1 {
            let m = vec![x, y];
            let b = vec![0, y - 1];
            if let Some((k, _)) = chinese_remainder_theorem(&b, &m) {
                assert_eq!(k % x, 0);
                assert_eq!(k % y, y - 1);
                assert_eq!((k * (k + 1) / 2) % n, 0);
                assert!(k > 0);
                ans = min(ans, k);
            }
        }
    }
    ans
}

pub fn extended_gcd(a: i128, b: i128, p: &mut i128, q: &mut i128) -> i128 {
    if b == 0 {
        *p = 1;
        *q = 0;
        a
    } else {
        let d = extended_gcd(b, a % b, q, p);
        *q -= a / b * *p;
        d
    }
}

pub fn chinese_remainder_theorem(b: &[i128], modulo: &[i128]) -> Option<(i128, i128)> {
    let (mut result, mut m) = (0, 1);
    for i in 0..b.len() {
        let (mut p, mut q) = (0, 0);
        let d = extended_gcd(m, modulo[i], &mut p, &mut q);
        if (b[i] - result) % d != 0 {
            return None;
        }
        let tmp = ((b[i] - result) / d * p) % (modulo[i] / d);
        result += m * tmp;
        m *= modulo[i] / d;
    }
    Some(((result % m + m) % m, m))
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}