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 <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

#include <atcoder/all>

using namespace atcoder;
//typedef modint998244353 mint;

int main(){
  ll N;
  cin>>N;
  ll NN=N;
  map<ll,int> M;
  rep(i,sqrt(N)){
    if(NN%(i+2)==0){
      while(NN%(i+2)==0){
        NN/=(i+2);
        M[i+2]++;
      }
    }
  }
  M[2]++;
  if(NN!=1) M[NN]++;
  ll num=M.size();
  ll ans=N*2-1;
  rep(i,(1ll<<num)){
    ll A=1;
    auto itr=M.begin();
    rep(j,num){
      if(i&(1ll<<j)){
        rep(k,itr->second){
          A*=(itr->first);
        }
      }
      itr++;
    }
    VL AA(2);
    AA[0]=A;
    AA[1]=N*2/A;
    VL BB(2);
    BB[0]=1;
    BB[1]=0;
    PLL Y=crt(BB,AA);
    if(Y.first>=2 and Y.first*(Y.first-1)%(N*2)==0) ans=min(ans,Y.first-1);
  }
  cout<<ans<<endl;
}