/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve2() {
	// for(int n = 1; n <= 1000; ++ n) {
    //     cout << n << " ";
    //     for(int k = 1; k <= 2000; ++ k) {
    //         if((k * (k + 1) / 2) % n == 0) {
    //             cout << k << endl;
    //             break;
    //         }
    //     }
    // }
}

ll pow(ll a, ll m) {
    ll res = 1LL;
    while(m > 0) {
        if(m & 1) res = res * a;
        a = a * a;
        m >>= 1;
    }
    return res;
}

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

void solve() {
    int n; cin >> n;
    if(n == 1) {
        cout << 1 << endl;
        return;
    }
    n *= 2LL;
    map<int, int> pf;
    map<int, int> pfs;
    int n_ = n;
    vector<int> ps;
    for(int i = 2; i * i <= n_; ++ i) {
        if(n_ % i == 0) {
            ps.pb(i);
            pfs[i] = 1LL;
        }
        while(n_ % i == 0) {
            pf[i] ++;
            pfs[i] *= i;
            n_ /= i;
        }
    }
    if(n_ != 1) {
        pf[n_] ++;
        pfs[n_] = n_;
        ps.pb(n_);
    }
    if((int)ps.size() == 1 and ps[0] == 2) {
        cout << n - 1 << endl;
        return;
    }
    int sz = (int)ps.size();
    int ans = INF;
    for(int bit = 0; bit < (1LL<<sz); ++ bit) {
        ll k = 1LL, k_ = 1LL;
        rep(i, sz) {
            if(bit & (1LL<<i)) {
                k *= pfs[ps[i]];
            } else {
                k_ *= pfs[ps[i]];
            }
        }
        int x, y;
        int d = extgcd(k_, -k, x, y);
        int res = k * y;
        // cerr << k << " " << k_ << " " << res << endl;
        if(res != 0 and res != 1) chmin(ans, res);
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

Measured memory_bytes: 3680