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<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
void exgcd(ll a, ll b, ll& x, ll& y, ll& c)
{
    if(!b) {y = 0; x = 1; c = a; return;}
    exgcd(b, a % b, y, x,c); y -= a / b * x;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll n,x,y,u,v,ans=-1,a,b;
ll solve(ll x,ll y)
{
	ll v,u,d;
	exgcd(x,y,u,v,d);
	if(u>0)u-=y*((u-1)/y+1);
	else u+=y*((-u)/y);
	if(v>0)v-=x*((v-1)/x+1);
	else v+=x*((-v)/x);
	return min(-u*x,-v*y);
}
int main()
{
	scanf("%lld",&n);n=2ll*n;
	ans=n-1;
	for(int i=2;1ll*i*i<=n;i++)
	{
		if(n%i)continue;
		x=i;y=n/i;
		if(gcd(x,y)>1)continue;
		a=solve(x,y),solve(y,x);
		ans=min(a,ans);
	}
	printf("%lld\n",ans);
	return 0;
}