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>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(),in.end()

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 (b < a) { a = b; return 1; } return 0; }

using ll = long long;
using ull = unsigned long long;
using namespace std;
using namespace atcoder;
// cout << fixed << setprecision(10）<<  << endl;

vector<pair<ll,ll>> prime(ll n) {
    vector<pair<ll,ll>> res;
    for (int i = 2; i * i <= n; i++) {
        if (n % i != 0) continue;
        ll count = 0;
        while (n % i == 0) { count++; n /= i; }
        res.push_back(make_pair(i,count));
    }
    if(n != 1) res.push_back(make_pair(n,1));
    return res;
}

ll pow(ll a, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1) res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}

ll GCD(ll a, ll b){
    ll s = a, t = b;
    while(s % t) { s %= t; swap(s,t); }
    return t;
}

const ll INF = 1ll<<60;

ll extGCD(ll a, ll b, ll &x, ll &y) {
    x = 1, y = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a,b);
        x -= t * y; swap(x,y);
    }
    return a;
}

int main(){
    ll n; cin >> n;
    auto p = prime(n * 2ll);
    vector<ll> ans;
    for (int i = 0; i < (int)p.size(); i++) {
        ll div = p[i].first, num = p[i].second;
        ans.push_back(pow(div, num));
    }
    ll res = n - 1ll; n *= 2;
    for(int i = 0; i < 1ll<<(int)ans.size(); i++) {
        ll c1 = 1ll;
        for (int j = 0; j < (int)ans.size(); j++) {
            if (i & 1ll<<j) c1 *= ans[j];
        }
      if (c1 == 1ll || c1 == n) continue; ll c2 = n / c1;
      if (c1 + 1 == c2) { cout << c1 << endl;
                         return 0;
                        }
        ll x, y;
        extGCD(c2, -c1, x, y);
        ll count = c2 * x;
        count = (count % n + n) % n;
        chmin(res, count);
    }
    cout << res << endl;
    return 0;
}