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: Accepted)

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(){
		/*
		for(long x=1;;x++){
			if(x % 1000 == 0) Console.WriteLine("x:{0}", x);
			Sol mySol =new Sol(x);
			mySol.Solve(true);
		}
		*/
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(bool assert = false){
		List<long> L = new List<long>();
		long N2 = N * 2;
		for(long x=1;x*x<=N2;x++){
			if(N2 % x == 0){
				L.Add(x);
				if(N2 / x != x) L.Add(N2 / x);
			}
		}
		
		//Console.WriteLine(String.Join(" ",L));
		
		long ans = (long) long.MaxValue;
		foreach(var p in L){
			long q = N2 / p;
			// k includes p, (k + 1) includes q;
			// <=> k = 0 mod p, (k + 1) = 0 mod q;
			if(gcd(p, q) != 1) continue;
			var k = MyMath.CRT(p, 0, q, q - 1);
			while(k <= 0) k += N2;
			//Console.WriteLine("{0} {1} {2}",p, q, k);
			ans = Math.Min(ans, (long) k);
		}
		if(!assert){
			Console.WriteLine(ans);
		} else {
			long kk = naive();
			if(kk != ans){
				Console.WriteLine("naive: {0}, ans: {1}", kk, ans);
				throw new Exception("aaa");
			}
		}
	}
	
	long naive(){
		long k = 0;
		long sum = 0;
		while(true){
			k++;
			sum += k;
			if(sum % N == 0) return k;
		}
	}
	
	long gcd(long a, long b){ return a == 0 ? b : gcd(b % a, a);}
	long N;
	public Sol(){
		N = rl();
	}
	public Sol(long x){
		N = x;
	}
	
	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 long CRT(long m1,long a,long m2,long b){
		// ChineseReminderTheorem
		// m1,m2:coprime
		// x%m1==a,x%m2==b => return x%m1m2
		//
		// ∃u,v  s.t.  u*m1+v*m2==1;
		//   return x=(b*u*m1+a*v*m2)%(m1m2);
		long u = 0, v = 0, c = 0;
		ExtGCD(m1, m2, ref u, ref v, ref c);
		long mm = m1 * m2;
		b *= u; b %= mm; b *= m1; b %= mm;
		a *= v; a %= mm; a *= m2; a %= mm;
		return (a + b) % mm;
	}


	public static void ExtGCD(long x,long y,ref long a,ref long b,ref long c){
		long r0 = x, r1 = y, a0 = 1, a1 = 0, b0 = 0, b1 = 1;
		long q1, r2, a2, b2;
		while(r1 > 0){
			q1 = r0 / r1;
			r2 = r0 % r1;
			a2 = a0 - q1 * a1;
			b2 = b0 - q1 * b1;
			r0 = r1; r1=r2;
			a0 = a1; a1=a2;
			b0 = b1; b1=b2;
		}
		c = r0;
		a = a0;
		b = b0;
	}
}
*/

class MyMath{
	public static BI CRT(BI m1,BI a,BI m2,BI b){
		// ChineseReminderTheorem
		// m1,m2:coprime
		// x%m1==a,x%m2==b => return x%m1m2
		//
		// ∃u,v  s.t.  u*m1+v*m2==1;
		//   return x=(b*u*m1+a*v*m2)%(m1m2);
		BI u = 0, v = 0, c = 0;
		ExtGCD(m1, m2, ref u, ref v, ref c);
		BI mm = m1 * m2;
		b *= u; b %= mm; b *= m1; b %= mm;
		a *= v; a %= mm; a *= m2; a %= mm;
		return (a + b) % mm;
	}


	public static void ExtGCD(BI x,BI y,ref BI a,ref BI b,ref BI c){
		BI r0 = x, r1 = y, a0 = 1, a1 = 0, b0 = 0, b1 = 1;
		BI q1, r2, a2, b2;
		while(r1 > 0){
			q1 = r0 / r1;
			r2 = r0 % r1;
			a2 = a0 - q1 * a1;
			b2 = b0 - q1 * b1;
			r0 = r1; r1=r2;
			a0 = a1; a1=a2;
			b0 = b1; b1=b2;
		}
		c = r0;
		a = a0;
		b = b0;
	}
}