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 PyPy3 (7.3.0) (judge verdict: Time Limit Exceeded)

n=int(input())
if n==1:
  print(1)
  exit()
def make_divisors(n):  
  d1=[]
  d2=[]
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      if i != n // i:
        if (n//i)%2==0 and i%2==0:continue
        d2.append(n//i)
        d1.append(i)
  # divisors.sort()
  return d1,d2
def gcd(a, b):
  while b:
    a, b = b, a%b
  return a

ans=n-1 if n%2==1 else 2*n-1
d1,d2=make_divisors(2*n)
#print(d1,d2)
for x,y in zip(d1,d2):
  if gcd(x,y)==1:
    xx,yy=x,y
    while abs(xx-yy)>1:
      d=abs(xx-yy)
      if xx>yy:yy+=max(y,y*(d//y))
      else:xx+=max(x,x*(d//x))
    ans=min(ans,xx,yy)
print(ans)