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 <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

ll extgcd(ll a,ll b,ll& x,ll& y){
	ll d=a;
	if(b){
		d=extgcd(b,a%b,y,x);
		y-=a/b*x;
	}
	else x=1,y=0;
	return d;
}

ll n;
vl a;

int main(){
	cin>>n;
	if(n==1){
		cout<<1<<endl;
		return 0;
	}
	n*=2;
	ll N=n;
	for(ll i=2;i*i<=n;i++) if(n%i==0){
		ll t=1;
		while(n%i==0) n/=i,t*=i;
		a.push_back(t);
	}
	if(n>1) a.push_back(n);
	ll m=a.size(),res=INF;
	for(int i=0;i<1<<m;i++){
		ll p=1;
		for(int j=0;j<m;j++) if(i&1<<j) p*=a[j];
		ll q=N/p,x,y;
		extgcd(p,q,x,y);
		x*=-1;
		if(x>0){
			if(p>INF/x+2) continue;
			res=min(res,p*x);
		}
	}
	cout<<res<<endl;
}