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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';

int const MAX_N = 32000000;
vector<long long> prime;
vector<bool> is_prime(MAX_N,true);
void Eratosthenes() {
    is_prime[0] = is_prime[1] = false;
    for (long long i = 2;i*i < MAX_N;++i) if (is_prime[i]) for (long long j = 2*i;j < MAX_N;j += i) is_prime[j] = false;
    for (long long i = 0;i < MAX_N;++i) if (is_prime[i]) prime.push_back(i);
}

vector<pair<long long,int>> prime_factorization(long long a) {
    vector<pair<long long,int>> prime_factor;
    if (is_prime[0]) Eratosthenes();
    for (long long& i : prime) {
        if (i*i > a) break;
        if (a%i == 0) {
            pair<long long,long long> p = make_pair(i,0);
            while (a%i == 0) {
                a /= i;
                p.second++;
            }
            prime_factor.push_back(p);
        }
    }
    if (a != 1) prime_factor.push_back(make_pair(a,1));
    return prime_factor;
}

void rec(vector<long long>& div,vector<pair<long long,int>>& fact,int i,long long d) {
    if (i == fact.size()) {
        div.push_back(d);
        return;
    }
    for (int j = 0;j <= fact[i].second;++j) {
        rec(div,fact,i+1,d);
        d *= fact[i].first;
    }
}

// unsorted
vector<long long> divisor(long long a) {
    vector<long long> div;
    vector<pair<long long,int>> prime_factor = prime_factorization(a);
    rec(div,prime_factor,0,1);
    return div;
}

ll extGcd(ll a,ll b,ll &p,ll &q) {
    if (b == 0) {
        p = 1;
        q = 0;
        return a;
    }  
    ll d = extGcd(b,a%b,q,p);  
    q -= a/b*p;  
    return d;  
}

int main() {
    Eratosthenes();
    ll n;
    cin >> n;
    vector<pair<long long,int>> p = prime_factorization(2*n);
    int m = p.size();
    ll ans = 1e18;
    for (int s = 0;s < (1<<m);++s) {
        ll u = 1,v;
        for (int j = 0;j < m;++j) if ((s>>j)&1) for (int i = 0;i < p[j].second;++i) u *= p[j].first;
        v = 2*n/u;
        ll x,y;
        extGcd(u,v,x,y);
        if (u*x == 0 || v*y == 0) continue;
        // cout << u*x << " " << v*y << endl;
        ans = min(ans,abs(x*u));
    }
    cout << ans << newl;
}