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 <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;
 
#ifdef DEBUG
    #include "debug.h"
    #define dump(...)                                                              \
        DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
                << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
                << endl                                                            \
                << "    ",                                                         \
            dump_func(__VA_ARGS__)
#else
    #define dump(...)
#endif

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

#define int long long
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

//*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)

vector<int> divisor(int n){
    vector<int> ret;
    for (int i = 1; i*i <= n; i++){
        if (n%i == 0){
            ret.push_back(i);
            if (i*i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

// ax+by=1の解
int extgcd(int a, int b, int& x, int& y){
    int d = a;
    if (b != 0){
        d = extgcd(b, a%b, y, x);
        y -= (a / b)*x;
    }
    else{
        x = 1; y = 0;
    }
    return d;
}

// mod mでのaの逆数
int mod_inv(int a, int m){
    int x, y;
    a = (a%m+m)%m;
    extgcd(a, m, x, y);
    return (m + x%m) % m;
}

int func(int N)
{
   int ans = INF;

   int c = N;
   while(c%2==0) c/=2;
   if(c==1){ return N*2-1;}
 
   auto d = divisor(N);
   //    dump(d);
   
   if(d.size()==2){
       return N-1;
   }
   
   for(auto p : d)
   {
       
       int q = N / p;
//       if(gcd(p,q)>1) continue;

       if(gcd(2*p,q)==1)
       {
           int i = mod_inv(-2*p,q);
           i = ((i%q)+q)%q;
           int k = i * 2*p;
//           dump("a",p,q,i,k);
           if(k!=0){
               if(k * (k+1)/2 % N==0) chmin(ans, k);
               else
                   dump("NG");
           }
       }
       
       if(gcd(p,2*q)==1)
       {
           q*=2;
           int i = mod_inv(-p,q);
           i = ((i%q)+q)%q;
           int k = i * p;
//           dump("b",p,q,k);
           if(k!=0){
               if(k * (k+1)/2 % N==0) chmin(ans, k);
               else
                   dump("NG");
           }
       }
   }
   return ans;
}

void solve(){

    int N;
    cin >> N;
    
    cout << func(N) << endl;
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed;
    solve();
}