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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using System.Numerics;
using CS=System.Runtime.CompilerServices;
//using System.Diagnostics;
using static System.Math;
using ll=System.Int64;
using static Contest_B.Lib_IO;
using static Contest_B.Lib_Minifunc;


namespace Contest_B {

public static class Entry
{

	public static void Main() {
		checked{
			long n;
			Lib_IO.rm(out n);


			ll ip = (ll)Sqrt(8*n);
			for (ll i = ip; i <= 100*n; i++)
			{
				ll i2 = i*i;
				if((i2-1)%(8*n)==0){
					wm((i-1)/2);
					return;
				}
			}

		}
	}


	public static long Legendre(long a, long p) {
		long ret = Pow(a, (p-1)/2, p);
		if (ret == p-1) ret = -1;
		return ret;
	}

	// Tonelli–Shanks algorithm　　calculate R such that R^2 = n (mod p)  p is a prime
	// See http://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm
	public static long ModSqrt(long n, long p) {
		if(p <2) return -1;
		if(p==2) return n;
		long r = Legendre(n, p);
		if(r!=1) return r;
		
		// step 1
		long Q = p-1;
		long S = 0;
		while ((Q & 1) == 0) {
			++S;
			Q >>= 1;
		}
		if (S == 1) return Pow(n, (p+1)/4, p);

		// step 2
		long z = 1;
		var rnd = new System.Random();
		int rm = int.MaxValue <= p ? int.MaxValue : (int)p+1;
	    while (Legendre(z, p) != -1)  z = rnd.Next(rm);
		long c = Pow(z, Q, p);
		
		// step 3
		long R = Pow(n, (Q+1)/2, p);
		long t = Pow(n, Q, p);
		long M = S;

		// step 4
		while (t != 1) {
			long i;
			long t2 = t;
			for (i = 1; i < M; i++) {
				t2 = t2 * t2 % p;
				if (t2 == 1) break;
			}
			long b = Pow(c, 1L<<(int)(M-i-1), p);
			R = R * b % p;
			t = (t * b % p) * b % p;
			c = b * b % p;
			M = i;
		}
		
		return R;
	}

	public static long Garner(long[] b, long[] m, bool needpre, long MOD=9223372036854775783L) {
		if(needpre){
			long lcm;
			if((lcm = PreGarner(ref b, ref m, MOD))==-1) return -1;
		}
		var q = new long[m.Length+1];
		Array.Copy(m,q,m.Length);
		q[q.Length-1] = MOD; // 番兵
		long[] coeffs = Enumerable.Repeat(1L,q.Length).ToArray();
		long[] constants = Enumerable.Repeat(0L,q.Length).ToArray();
		for (int k = 0; k < b.Length; ++k) {
			long t = (b[k] - constants[k]) * ModInv(coeffs[k], q[k]) % q[k];
			t += (t<0)?q[k]:0;
			for (int i = k+1; i < q.Length; ++i) {
				constants[i] = (constants[i] + t * coeffs[i]) % q[i];
				coeffs[i] = (coeffs[i] * q[k]) % q[i];
			}
		}
		return constants[constants.Length-1];
	}
	// Garner のアルゴリズムの前処理 mが互いに素ではない場合
	public static long PreGarner(ref long[] b, ref long[] m, long MOD=9223372036854775783L) {
		for (int i = 0; i < b.Length; ++i) {
			for (int j = 0; j < i; ++j) {
				long g = GCD(m[i], m[j]);
				// これを満たさなければ解はない
				if ((b[i] - b[j]) % g != 0) return -1;
				// s = m[i], t = m[j] を仮想的に素因数分解して s = p^k ... q^l ..., t = q^m ... r^n ... となったときに
				m[i] /= g; // p については i の方が大きかったものについての j との差分、と q
				m[j] /= g; // p については j の方が大きかったものについての i との差分、と r
				// 残る g を i と j に振り分ける (i の方が指数大きかった素因子 p の分は最終的に gi に、j の方が指数大きかった素因子 p の分は最終的に gj に)
				// ひとまず j 側にある p については gj のみに行くようにする
				long gi = GCD(m[i], g), gj = g/gi;
				// 本来 i 側に行くべき p で gj 側にあるものを gi 側に寄せていく
				do {
					g = GCD(gi, gj);
					gi *= g; gj /= g;
				} while (g != 1);
				// i 側と j 側に戻していく
				m[i] *= gi; m[j] *= gj;
				// m[i] と m[j] が元より小さくなったのに合わせて余りも計算し直しておく
				b[i] %= m[i]; b[j] %= m[j];
			}
		}
		var bb = new List<long>();
		var mm = new List<long>();
		for (int i = 0; i < b.Length; i++)
		{
			if(1<m[i]){
				bb.Add(b[i]);
				mm.Add(m[i]);
			}
		}
		b = bb.ToArray();
		m = mm.ToArray();
		long lcm = 1;
		for (int i = 0; i < b.Length; ++i) lcm = (lcm * m[i]) % MOD;
		return lcm;
	}

	public static long[] PrimeFactor(long n) {
		var ret = new List<long>();
		if (n < 2) return ret.ToArray();
		while ((n & 1) == 0) {
			ret.Add(2);
			n >>= 1;
		}
		while ((n % 3) == 0) {
			ret.Add(3);
			n /= 3;
		}
		long sqi = Convert.ToInt64(Math.Sqrt(n));
		while(sqi*sqi<=n) sqi++;
		for (long i = 5, f = 2; i < sqi; i += f, f ^= 6) {
			while ((n % i) == 0) {
				ret.Add(i);
				n /= i;
			}
			if(n==1) break;
		}
		if(1<n) ret.Add(n);
		return ret.ToArray();
	}

	public static long Pow(long x, long n, long m=long.MaxValue) {
		long ret = 1;
		while (true) {
			if ((n & 1) == 1) ret = (ret * x) % m;
			if((n >>= 1)<=0) break;
			x = (x * x) % m;
		}
		return ret;
	}

	public static long GCD(long a, long b) {
		while (b != 0) { var t = b; b = a % b; a = t; }
		return a;
	}
	public static long GCD(params long[] z) { long v = z[0]; for(int i=1;i<z.Length;i++) v = GCD(v, z[i]); return v; }
	public static long LCM(long a, long b) => checked(a / GCD(a, b) * b);
	public static long LCM(params long[] z) { long v = z[0]; try{ for(int i=1;i<z.Length;i++) v = LCM(v, z[i]); }catch{ v=long.MaxValue; } return v; }

	
	// 拡張ユークリッドの互除法   返り値: a と b の最大公約数  ax + by = gcd(a, b) を満たす (x, y) が格納される
	public static long EGCD(long a, long b, out long x, out long y) {
		x = 0; y = 1;
		long u = 1 , v = 0, gcd = 0;
		while (a != 0) {
			var q = b / a; var r = b % a; var m = x - u * q; var n = y - v * q;
			b = a; a = r; x = u; y = v; u = m; v = n; gcd = b;
		}
		return gcd;
	}

	// 逆元
	public static long ModInv(long value, long modulo) {
		long x, y;
		if (1 != EGCD(value, modulo, out x, out y)) throw new ArgumentException("Invalid modulo", "modulo");
		return (x < 0)?x+modulo:x;
	}

	public static KeyValuePair<T,long>[] ElementCount<T>(IEnumerable<T> a, long order=0) where T : IComparable<T> {
		var d = new Dictionary<T, long>();
		foreach (var e in a) {
			if(!d.ContainsKey(e)) d[e] = 0;
			d[e]++;
		}
		var r = d.ToArray();
		switch(order) {
			case  1: Array.Sort(r,(x,y)=>(x.Key.CompareTo(y.Key))); break;
			case -1: Array.Sort(r,(x,y)=>(y.Key.CompareTo(x.Key))); break;
			case  2: Array.Sort(r,(x,y)=>(x.Value.CompareTo(y.Value))); break;
			case -2: Array.Sort(r,(x,y)=>(y.Value.CompareTo(x.Value))); break;
			case  3: Array.Sort(r,(x,y)=>(x.Value!=y.Value?x.Value.CompareTo(y.Value):x.Key.CompareTo(y.Key))); break;
			case  4: Array.Sort(r,(x,y)=>(x.Value!=y.Value?x.Value.CompareTo(y.Value):y.Key.CompareTo(x.Key))); break;
			case -3: Array.Sort(r,(x,y)=>(x.Value!=y.Value?y.Value.CompareTo(x.Value):x.Key.CompareTo(y.Key))); break;
			case -4: Array.Sort(r,(x,y)=>(x.Value!=y.Value?y.Value.CompareTo(x.Value):y.Key.CompareTo(x.Key))); break;
			default: break;
		}
		return r;
	}



}



#region BaseModule


public static class Lib_Minifunc{
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
	[CS.MethodImpl(256)] public static void swap1<T>(ref T a,ref T b) { T t = a; a = b; b = t; }
	[CS.MethodImpl(256)] public static void swap2<T>(ref T a,ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換

	[CS.MethodImpl(256)] public static bool chmin<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)== 1)) a = b; return r; }  // b の方が小さければ a を更新
	[CS.MethodImpl(256)] public static bool chmax<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)==-1)) a = b; return r; }  // b の方が大きければ a を更新
	[CS.MethodImpl(256)] public static bool inside(long i,long n) => (0<=i&&i<n);
	[CS.MethodImpl(256)] public static bool inside(long x,long y,long w,long h) => (inside(x,w)&&inside(y,h));
	[CS.MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmin(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmax(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static long mod(long a,long m=MOD1) { var v = a%m; return (v<0?(m<0?v-m:v+m):v); }
	[CS.MethodImpl(256)] public static long ceil(long a,long b) => (a%b==0||(a<0&&0<b)||(0<a&&b<0)?a/b:a/b+1);	// 整数商の切り上げ
	[CS.MethodImpl(256)] public static long round(double a) => Convert.ToInt64(a);	// 四捨五入
	public static string YES(bool a) => a?"YES":"NO";
	public static string yes(bool a) => a?"Yes":"No";
	[CS.MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[CS.MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[CS.MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	public static T[] initc<T>(long l1) where T : new() { var m = new T[l1]; for (int i=0;i<m.Length;i++) m[i] = new T(); return m; }	// new制約は遅い
	public static int[] range(long s, long c) => Enumerable.Range((int)s,(int)c).ToArray();
	[CS.MethodImpl(256)] public static T[] inita<T>(long l1,T v=default(T)) => Enumerable.Repeat(v,(int)l1).ToArray();
	public static T[][] inita<T>(long l1,long l2,T v=default(T)) { var m = new T[l1][]; for (int i=0;i<m.Length;i++) m[i] = inita(l2,v); return m; }
	public static void shift(this IList<int> l,int o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	public static void shift(this IList<long> l,long o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	[CS.MethodImpl(256)] public static bool initd<T,U>(this IDictionary<T,U> d, T k, U v=default(U)) { if(d.ContainsKey(k)) { return false; } else { d[k] = v; return true; } }
	public static void sort<T>(this T[] a,int o=1, Comparison<T> f=null) where T : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.CompareTo(y));break;
			case -1: Array.Sort(a,(x,y)=>y.CompareTo(x));break;
			default: break;
		}
	}
	public static void sort<T,U>(this P<T,U>[] a,int o, Comparison<P<T,U>> f=null) where T : IComparable where U : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			default: break;
		}
	}
	public static void sort<T,U,V>(this P<T,U,V>[] a,int o, Comparison<P<T,U,V>> f=null) where T : IComparable where U : IComparable where V : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			default: break;
		}
	}
	public static void sort<T,U,V,W>(this P<T,U,V,W>[] a,int o, Comparison<P<T,U,V,W>> f=null) where T : IComparable where U : IComparable where V : IComparable where W : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			case  4: Array.Sort(a,(x,y)=>x.D.CompareTo(y.D));break;
			case -4: Array.Sort(a,(x,y)=>y.D.CompareTo(x.D));break;
			default: break;
		}
	}
}


public struct P<T,U>
{
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V>
{
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W>
{
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO
{
	class Prt : System.IO.StreamWriter
	{
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[CS.MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[CS.MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[CS.MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = new T[z.Length];
		for(int i=0;i<a.Length;i++) a[i] = cv<T>(z[i]);
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[CS.MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			if(a.Count==0){
				Console.WriteLine();
			}else{
				foreach(T x in a) Console.WriteLine(f,x);
			}
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				if(b.Count==0){
					Console.WriteLine();
				}else{
					for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
					Console.WriteLine(f,b[b.Count-1]);
				}
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}