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 (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>

int factorize(long long n, long long p[], int m[])
{
	int k;
	long long i;
	for (i = 2, k = 0; i * i <= n; i++) {
		if (n % i != 0) continue;
		p[k] = i;
		for (m[k] = 0; n % p[k] == 0; m[k]++, n /= p[k]);
		k++;
	}
	if (n > 1) {
		p[k] = n;
		m[k++] = 1;
	}
	return k;
}

// Find an integer x such that ax + by = c with some integer y
long long solve(long long a, long long b, long long c)
{
	if (b % a == 0) return c / a;
	else return (c - solve(b % a, a, c % a) * b) / a;
}

int main()
{
	long long N;
	scanf("%lld", &N);
	if (N == 1) {
		printf("1\n");
		fflush(stdout);
		return 0;
	}

	long long p[101];
	int m[101], k = factorize(N * 2, p, m);
	
	int i, j, l[101] = {};
	long long x = 1, y, ans = N * 2, tmp;
	m[k] = 1;
	while (l[k] == 0) {
		y = N * 2 / x;
		tmp = solve(x, y, 1);
		if (tmp < 0) tmp = (tmp % (N * 2) + N * 2) % (N * 2);
		tmp = (x * tmp + N * 2 - 1) % (N * 2);
		if (tmp > 0 && ans > tmp) ans = tmp;
		
		for (i = 0; i < k && l[i] == m[i]; i++);
		for (j = 0; j < m[i]; j++) x *= p[i];
		for (l[i] = m[i], i--; i >= 0; l[i--] = 0) {
			for (j = 0; j < m[i]; j++) x /= p[i];
		}
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}