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 JavaScript (Node.js 12.16.1) (judge verdict: Time Limit Exceeded)

"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(){return +cin[cid++];}
function nextstr(){return cin[cid++];}
function nextbig(){return BigInt(cin[cid++]);}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextssort(n,a){return a?cin.slice(cid,cid+=n).map(a=>+a).sort((a,b)=>b-a):cin.slice(cid,cid+=n).map(a=>+a).sort((a,b)=>a-b);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}
var streams=[];function print(s){streams.push(s);}

var n,n2,b2,a={};

var myOut = main();
if(myOut !== undefined)console.log(String(myOut));
if(streams.length)console.log(streams.join("\n"));

function main(){
  n = next();
  n2 = 2*n;
  b2 = BigInt(n2);
  for(var i = 2; i *i <= n2; i++){
    if(n2 % i === 0){
      a[i] = 1, n2 /= i;
      while(n2 % i === 0)a[i]++, n2 /= i;
    }
  }
  if(n2 > 1)a[n2] = 1;
  var x = 1;
  for(var i in a){
    x *= (a[i]+1) >> 1;
  }
  n2 = 2*n;
  for(var i = x; i < n2; i += x){
    if(f(i-1))return i-1;
    if(f(i))return i;
  }
  return n2-1;
}

function f(k){
  var kk = k*(k+1);
  if(kk < n2)return;
  if(kk < 9e15){
    if(kk % n2=== 0)return 1;
  }else{
    kk = BigInt(k)*BigInt(k+1);
    if(kk % b2 === 0n)return 1;
  }
}