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 f(i,a,b) for( ll i = a; i <(ll) b ; i++ ) 
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define inter ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)

// policy-based
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;	


using namespace std;
using namespace atcoder;

typedef long long ll; // int or long long
typedef long double ld;
typedef pair<ll,ll> ii ;
typedef vector<ll>  vi ;
typedef vector<ii> vii ;

/*
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
*/
 
const ll MAX = 2e7 ;
const ll inf = 1e18;
const ll mod = 1e9 + 7;

int main(){
	fastio;	
//	ll n,a,b;
//	cin >> n;
	ll n;
	f(tc,1,2){
		cin >> n;
		ll ans = (n&1?n-1:2*n -1);
		if(n == 1){
			cout << "1\n";
		} 
		else{

			if(n%2 == 0) n*=2;
			for(ll i=1; i*i<=n;i++){
				if(n%i == 0 && __gcd(i,n/i) == 1){
					ii aux;

					aux = crt({0,i},{n/i -1,n/i});
					ans = min(ans,aux.ff);
				
					if(i == 1) continue;
					aux = crt({0,n/i},{i -1,i});
					ans = min(ans,aux.ff);					
				}
			}
			if(n%2 == 0) n/=2;

			cout << ans << endl;
		}
	}
}