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
pf = Prime.instance.prime_division(n * 2)
pf.map! { |a, b| a ** b }

def each_subset(ary, &block)
  1.upto(ary.size - 1) { |n| ary.combination(n, &block) }
end

candidates = []
each_subset(pf) { |ary|
  a = ary.inject(1, :*)
  b = n * 2 / a

  # find a.x - b.y = 1
  s, t, x, x0, y, y0 = a, b, 1, 0, 0, 1
	until t.zero?
		s, (q, t) = t, s.divmod(t)
		x, x0 = x0, x - q * x0
		y, y0 = y0, y - q * y0
	end

  candidates << a * x << b * y
}

p candidates.map(&:abs).min