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 <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#include <iomanip>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head


#define ctz __builtin_ctzll
mt19937 rd(time(0));
const int p[12]={2,3,5,7,11,13,17,19,23,29,31,37};
const int p_cnt=12;

ll mmul(ll a,ll b,ll m) { //a*b%m
    ll d=((long double)a/m*b+1e-8);
    ll r=a*b-d*m;
    return r<0?r+m:r;
}
ll FastPow(ll a,ll b,ll m) {
    ll ans=1;
    for (;b;b>>=1,a=mmul(a,a,m))
        if (b&1) ans=mmul(ans,a,m);
    return ans;
}
ll gcd(ll a,ll b) {
    if (!a||!b) return a+b;
    int t=ctz(a|b); a>>=ctz(a);
    do {
        b>>=ctz(b);
        if (a>b) swap(a,b);
        b-=a;
    } while (b);
    return a<<t;
}

int isprime(ll n) {
    if (n==1) return 0;
    if (n==2||n==3||n==5) return 1;
    if (!(n&1)||!(n%3)||!(n%5)) return 0;

    ll m=n-1; int k=0;
    while (!(m&1)) m>>=1,++k;
    for (int ip=0;ip<p_cnt&&p[ip]<n;++ip) {
        ll x=FastPow(p[ip],m,n),y=x;
        for (int i=0;i<k;++i) {
            x=mmul(x,x,n);
            if (x==1&&y!=1&&y!=n-1) return 0;
            y=x;
        }
        if (x!=1) return 0;
    }
    return 1;
}

ll f[110];
int cnt;

ll g(ll x,ll n,ll a) {
    ll t=mmul(x,x,n)+a;
    return t<n?t:t-n;
}

const int M=(1<<7)-1;
ll Pollard_Rho(ll n) {
    if (!(n&1)) return 2;
    if (!(n%3)) return 3;
    if (!(n%5)) return 5;

    ll x=0,y=x,t=1,q=1,a=(rd()%(n-1))+1;
    for (int k=2;;k<<=1,y=x,q=1) {
        for (int i=1;i<=k;++i) {
            x=g(x,n,a);
            q=mmul(q,abs(x-y),n);
            if (!(i&M)) {
                t=gcd(q,n);
                if (t>1) break;
            }
        }
        if (t>1||(t=gcd(q,n))>1) break;
    }
    if (t==n) {
        t=1;
        while (t==1) t=gcd(abs((x=g(x,n,a))-y),n);
    }
    return t;
}

void work(ll n) {
    if (n==1) return;
    if (isprime(n)) f[++cnt]=n;
    else {
        ll t=n;
        while (t==n) t=Pollard_Rho(n);
        work(t),work(n/t);
    }
}

ll n,ans;
int chk(ll num) {
	if (num<=0) return 0;
	ll nn = n;
	ll x = num, y = num+1, z = 2;
	if (x%z==0) x/=z,z=1;
	if (y%z==0) y/=z;
	ll g = gcd(x,nn);
	nn /= g;
	return y%nn==0;
}
const int Z = 111;
void dfs(int d, ll num) { 
	if (d>cnt) {
		for (ll x=1; x<=100; ++x) {
			ll w = num*x;
			if (ans<=w) return;
			if (chk(w)) ans=w;
		}
		return;
	}
	while (n%num==0) {
		dfs(d+1,num);
		num *= f[d];
	}
}

int main() {
	ans=1e18;
	scanf("%lld", &n);
	cnt = 0;
	work(n);
	sort(f+1,f+1+cnt);
	cnt=unique(f+1,f+1+cnt)-f-1;
	dfs(1,1);
	printf("%lld\n", ans);
}