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# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BI = System.Numerics.BigInteger;


class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	const int M = 1 << 20;
	public void Solve(){
		
		bool[] isPrime = new bool[M];
		for(int i=2;i<M;i++) isPrime[i] = true;
		for(int i=2;i<M;i++){
			if(!isPrime[i]) continue;
			for(int j=i+i;j<M;j+=i) isPrime[j] = false;
		}
		
		List<long> L = new List<long>();
		for(long i=2;i*i<=M;i++){
			if(N % i == 0){
				L.Add(i);
				if(N / i != i) L.Add(N / i);
			}
		}
		
		long n = N;
		for(int i=2;i<M;i++){
			if(!isPrime[i]) continue;
			while(n % i == 0) n /= i;
		}
		
		List<long> P = new List<long>();
		if(n > 1){
			if(MyMath.MillerRabinTest(n)){
				P.Add(n);
			} else {
				long l = 0, r = (long)4e7;
				while(r - l > 1){
					var c = (l + r) / 2;
					if(c * c < n){
						l = c;
					} else {
						r = c;
					}
				}
				
				if(r * r == n){
					P.Add(r);
					P.Add(r);
				} else {
					P = Rho(n).Select(n => (long) n).ToList();
				}
			}
		}
		
		P.Add(1);
		P.Add(N);
		
		var hs = new HashSet<BI>();
		for(BI v = 1; v <= 2; v++){
			foreach(var nn in L){
				foreach(var p in P){
					BI k = v * nn * p;
					if((k * (k + 1) / 2 ) % (BI)N == 0) hs.Add(k);
					if((k * (k - 1) / 2 ) % (BI)N == 0) hs.Add(k-1);
				}
			}
		}
		Console.WriteLine(hs.ToArray().Min());
		
	}
	long N;
	public Sol(){
		N = rl();
	}
	
	BI gcd(BI a, BI b){ return a == 0 ? b : gcd(b % a, a);}
	List<BI> Rho(BI n){
		int[] seed = new int[]{3,5,7,11,13,17};
		var hs = new HashSet<BI>();
		foreach(var s in seed){
			BI x = 2, y = 2, d = 1;
			Func<BI,BI> f = x => ((x * x)+ s) % n;
			while(d == 1){
				x = f(x);
				y = f(f(y));
				d = gcd(BI.Abs(x - y), n);
			}
			if(d == n) continue;
			if(n % d == 0){
				hs.Add(d); hs.Add(n / d);
			}
		}
		return hs.ToList();
		
	}
	
	
	
	
	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class MyMath{
	
	public static bool IsPrime(BI x){
		if(x==2)return true;
		if((x==1) || (x&1)==0)return false;
		if(x<(BI)1e12){
			for(BI t=2;t*t<=x;t++){
				if(x%t==0)return false;
			}
			return true;
		}
		return MillerRabinTest(x);
	}
	
	public static bool MillerRabinTest(BI n){
		int rep=20;
		BI d=n-1;
		// (n-1)=2^s*d
		while((d&1)==0)d>>=1;
		
		var rnd=new Xor128();
		BI a=1+((BI)rnd.Next())%(n-2);
		
		while(rep>0){
			rep--;
			a=1+((BI)rnd.Next()*a)%(n-2);
			BI t=d;
			BI y=ModPowBI(a,t,n);
			while(t!=(n-1) && y!=1 && y!=n-1){
				y=y*y%n;
				t<<=1;
			}
			if(y!=(n-1) && (t&1)==0)return false;
		}
		return true;
	}
	
	static BI ModPowBI(BI a,BI t,BI mod){
		BI ret=1;
		while(t>0){
			if((t&1)==1){ret*=a;ret%=mod;};
			a=a*a;a%=mod;
			t>>=1;
		}
		return ret;
	}

}


class Xor128{
	uint x = 123456789;
	uint y = 362436069;
	uint z = 521288629;
	uint w = 88675123; 
	uint t;
	
	public Xor128(){
	}
	
	public Xor128(uint seed){
		z ^= seed;
		z ^= z >> 21; z ^= z << 35; z ^= z >> 4;
	}
	
	
	public uint Next(){
		t = x ^ (x << 11);
		x = y; y = z; z = w;
		return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
	}
	public int Next(int ul){
		return NextI(0,ul-1);
	}
	public int NextI(int from,int to){
		int mod=to-from+1;
		int ret=(int)(Next()%mod);
		return ret+from;
	}
}