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 with AC Library v1.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}

void solve(){
    ll n,ans{LLONG_MAX};
    cin>>n;
    for(ll i{1};i*i<=n;++i) if(n%i==0){
            ll tmp,_;
            tie(tmp,_)=crt(vector<ll>{0,-1},vector<ll>{i,n/i*2});
            if(tmp) chmin(ans,tmp);
            tie(tmp,_)=crt(vector<ll>{0,-1},vector<ll>{i*2,n/i});
            if(tmp) chmin(ans,tmp);
            tie(tmp,_)=crt(vector<ll>{0,-1},vector<ll>{n/i,i*2});
            if(tmp) chmin(ans,tmp);
            tie(tmp,_)=crt(vector<ll>{0,-1},vector<ll>{n/i*2,i});
            if(tmp) chmin(ans,tmp);
    }
    cout<<ans<<'\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
}