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.

Examples

Input

11


Output

10


Input

20200920


Output

1100144
#include<bits/stdc++.h>
using namespace std;
long long n,ans;
void exgcd(long long a,long long b,long long &d,long long &x,long long &y)
{
	if(!b)
		d=a,x=1,y=0;
	else 
		exgcd(b,a%b,d,y,x),y-=(a/b)*x;
}
int main()
{
	scanf("%lld",&n);
	n*=2;
	ans=n-1;
	for(long long i=1;i*i<=n;i++)
	{
		if(n%i==0)
		{
			long long d,x,y;
			exgcd(i,n/i,d,x,y);
			if(d>1)
				continue;
			if(x>0&&i*x-1>0)
				ans=min(ans,i*x-1);
			if(x<0&&i*(-x)>0)
				ans=min(ans,i*(-x)); 
		}
	}
	printf("%lld\n",ans);
	return 0;
}
