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>
using namespace std;
typedef long long ll;


ll is_pow2(ll k){
	ll a = sqrt(k);
	if(a*a==k){
		return a;
	}
	if((a+1)*(a+1)==k){
		return a+1;
	}
	if((a-1)*(a-1)==k){
		return a-1;
	}
	return 0;
}


int main(){

	ll N;
	cin >> N;

	for(ll a=1;a<=N;a++){
		//ll x = sqrt(8LL*a*N+1) - 1;
		ll sq = is_pow2(8LL*a*N+1);
		if(sq > 1){
			ll x = sq - 1;
			if(x % 2  ==  0){
				x/=2;
				cout << x << endl;
				return 0;
			} 
		}

	}
}