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>
#define int long long
int n,tot,ans,a[100];
inline void exgcd(int a,int b,int &x,int &y) {
	if(!b) {
		x=1;
		y=0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=a/b*x;
}
inline int solve(int a,int b) {
	int x,y;
	exgcd(a,b,x,y);
	x=(x%b+b)%b;
	if(!x)x=b;
	y=(1-a*x)/b;
	y=-y;
	if(y<=0) return n;
	return a*x;
}
signed main() {
	std::cin>>n,ans=2*n;
	if(n==1) return puts("1"),0;
	n=n*2;
	int tmp=n;
	for(int i=2; i*i<=tmp; ++i) {
		if(tmp%i==0) {
			++tot,a[tot]=1;
			while(tmp%i==0) tmp/=i,a[tot]*=i;
		}
	}
	if(tmp) a[++tot]=tmp;
	for(int up=0; up<(1<<tot)-1; ++up) {
		int x=1,y=1;
		for(int j=0; j<tot; ++j)
			if(up>>j&1)x*=a[j+1];
			else y*=a[j+1];
		ans=std::min(ans,std::min(solve(x,y),solve(y,x)));
	}
	std::cout<<ans-1<<'\n';
	return 0;
}