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: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define vb vector <bool>
#define vvb vector <vb>
#define mp make_pair
#define vvll vector <vll>
#define vsi vector <si>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define MANX MAXN
#define itn int
#define in(x); { for (auto &to : x) cin>>to;}
#define out(x); { for (auto &to : x) cout<<to<<" ";cout<<'\n'; }

const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const ll mod1=1e9+7;
const ll mod2=2e9+11;
__int128 bin_pow(__int128 base,__int128 deg,__int128 mod)
{
    if (deg==0)
        return 1;
    __int128 t=bin_pow(base,deg/2,mod);
    if (deg&1)
        return t*t%mod*base%mod;
    else
        return t*t%mod;
}
void solve()
{
    ll n;
    cin>>n;
    n*=2;
    ll was=n;
    vector <pll> p;
    vector <ll> x;
    for (ll i=2;i*i<=n;i++)
    {
        if (n%i==0)
        {
            int cnt=0;
            ll y=1;
            while (n%i==0)
                cnt++,n/=i,y*=i;
            x.pb(y);
            p.pb({i,cnt});
        }
    }
    if (n>1)
        p.pb({n,1}),x.pb(n);
    int sz=p.size();
    ll ans=INFLL;
    for (int mask=0;mask<(1<<sz);mask++)
    {
        __int128 a=1;
        for (int i=0;i<sz;i++)
        {
            if ((mask>>i)&1)
                a*=x[i];
        }
        __int128 b=was/a;
        __int128 phi=1;
        for (int i=0;i<sz;i++)
        {
            if ((mask>>i)&1)
                continue;
            phi*=x[i];
            phi/=p[i].fi;
            phi*=p[i].fi-1;
        }
        __int128 now=b-1;
        now=(now*bin_pow(a,phi-1,b))%b;
        if (now==0)
            ans=min(ans,(ll)(a*b));
        else {
            ans = min(ans, (ll)(now * a));
        }
    }
    cout<<ans<<'\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifdef Mip182
    freopen("a.in","r",stdin);
    #endif
    int _t;
    _t=1;
  //  cin>>_t;
    while (_t--)
        solve();
    //n=1 ?
    //recursion - matrix exponential
    //linear algebra - just a joke
    //check ios_base, MAXN, mod1 before submit
    //if debug more than 15 minutes - write stress
    //compression
}