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 Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	long N;

	Main(BufferedReader in) throws IOException {
		N = Long.parseLong(in.readLine());
	}

	long isOK(long val) {
		long x = (long) Math.sqrt((double) val);
		for (long i = x;; ++i) {
			if (i * (i + 1) > val) {
				return -1L;
			} else if (i * (i + 1) == val) {
				return i;
			}
		}
	}

	void calc() {
		long result = -1;
		for (long x = 1;; ++x) {
			long v = isOK(2L * N * x);
			if (v > 0) {
				result = v;
				break;
			}
		}
		System.out.println(result);
	}

	void showResult() {
	}
}