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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    n *= 2;
    ll ans = n - 1;
    REP(i,2,50000000) {
        if(n%i)continue;
        if(i>=n)break;
        ll x = i, y = n/i;
        rep(_,2){
            ll a=0,b=0;
            ll g = extGCD(x,y,a,b);
            if(g!=1)continue;
            while(a<0){
                a+=y;b-=x;
            }
            while(a>y){
                a-=y;b+=x;
            }
            assert(a*x + b* y == 1);
            if(1.0 * a * x > 1e18)continue;
            ans = min(ans, a*x);

            a=-a;b=-b;
            a+=y;b-=x;
            assert(a*x + b* y == -1);
            if(1.0 * a * x > 1e18)continue;
            ans = min(ans, a * x);
            swap(x,y);
        }
    }
    cout << ans << endl;
    return 0;
}