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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());

            var f = 0;
            long m = (long)Math.Sqrt(8 * n + 1);
            while (f == 0) 
            {
                if((m * m - 1) % (8*n) == 0)
                {
                    Console.WriteLine((m - 1) / 2);
                    return;
                }
                else
                {
                    m++;
                }
            }

            /*
            bool IsSquared(long k)
            {
                var x = Math.Sqrt((double)k);
                if (x == (long)x) 
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            var frag = 0;
            var m = 8 * n + 1;

            while(frag == 0)
            {
                if(IsSquared(m))
                {
                    frag = 1;
                    break;
                }

                else
                {
                    m += 8 * n;
                }
            }
            */

        }
    }
}