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: Wrong Answer)

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>
#include<atcoder/all>
using namespace atcoder;

using namespace std;
//#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define debug(output) cout<<#output<<"= "<<output<<endl

using P=pair<int,int>;
using lint=long long;
using ll=long long;
const lint inf=1e18+7;
const int MOD=1000000007;

long long myPow(lint x,lint n){
    lint res=1;
    lint cur=x;
    while(n>0){
        if(n&1){
            res*=cur;
        }
        n/=2;cur*=cur;
    }
    return res;
}
signed main(){
  lint n;cin>>n;
  map<lint,int> prime;
  lint bbuf=n*2;
  for(lint i=2;i*i<=n*2;++i){
    if(bbuf%i==0){
      bbuf/=i;
      prime[i]+=1;
    }
  }
  if(bbuf>1)prime[bbuf]+=1;

  vector<pair<lint,int>> primes;
  for(auto& val:prime){
    primes.push_back({val.first,val.second});
  }
  lint num=primes.size();
  lint res=inf;
  //if(n%2==1)res=n-1;
  //else res=2*n-1;
  for(lint bit=0;bit<(1<<num);++bit){
    lint a=1;lint b=1;
    for(int i=0;i<num;++i){
      if(bit&(1<<i)){
        a*=myPow(primes[i].first,primes[i].second);
      }
      else{
        b*=myPow(primes[i].first,primes[i].second);
      }
    }
    vector<lint> r(2),m(2);
    r[0]=0;m[0]=a;
    r[1]=-1;m[1]=b;
    auto p=crt(r,m);
    if(p.first==0)p.first=n*2;
    res=min(res,p.first);
  }
  cout<<res<<"\n";
  return 0;
}