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 Ruby (2.7.1) (judge verdict: Wrong Answer)

require "prime"
N=gets.to_i
pd=N.prime_division
#warn pd.inspect
pd.map!{|a,b|a**b}
warn pd.inspect
min=(2*N)**2
def modinv(a,m)
	b=m
	u=1
	v=0
	while b>0
		t=a/b
		a-=t*b
		s=a
		a=b
		b=s
		u-=t*v
		s=u
		u=v
		v=s
	end
	u%=m
	if u<0
		u+=m
	end
	u
end
(2**pd.size).times{|i|
	k=1
	k1=1
	pd.size.times{|j|
		if i[j]==0
			k*=pd[j]
		else
			k1*=pd[j]
		end
	}
	warn [k,k1].inspect
	if k==1
		k=k1-1
	elsif k1==1
		k1=k+1
	else
		x=modinv(k1,k)
		k1*=x
		y=(k1-1)/k
		k=k1-1
		if x*y%2==1
			if x<=y
				k*=2
			else
				k1*=2
				k=k1-1
			end
		end
		#warn [x,k1,y,k].inspect
	end
	if k<min
		min=k
	end
}
puts min