//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

template<typename T>
class prime {
    T n;
public:
    prime(T n) : n(n) {}
    
    map<T, int> factorlist() {
        if (n == 1) return {};
        T nn = n;
        map<T, int> ret;
        for (int i = 2; (ll) i * i <= n; i++) {
            if (nn % i != 0) continue;
            int cnt = 0;
            while (nn % i == 0) {
                cnt++;
                nn /= i;
            }
            ret[i] = cnt;
        }
        if (nn != 1) ret[nn]++;
        return ret;
    }
    
    vector<T> unique_factor() {
        map<T, int> m = factorlist();
        vector<T> ret;
        for (auto p : m) ret.pb(p.first);
        return ret;
    };
    
    bool isPrime() {
        auto v = factorlist();
        return v.size() == 1 && v.begin()->second == 1;
    }
    
    int count_divisor() {
        int ret = 1;
        map<int, int> fl = factorlist(n);
        for (auto p : fl) ret *= p.second + 1;
        return ret;
    };
    
    vector<T> enum_divisors() {
        vector<T> res;
        for (T i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                res.pb(i);
                if (n / i != i) res.pb(n / i);
            }
        }
        sort(all(res));
        return res;
    }
};

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() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    if(n == 1) {
        cout << 1 << endl;
        return 0;
    }
    if (~n & 1) n *= 2;
    prime<ll> pr(n);
    auto mp = pr.factorlist();
    int sz = mp.size();
    ll ans = n-1;
    rep(bit, 1 << sz) {
        ll l = 1, r = 1;
        auto it = mp.begin();
        rep(i, sz) {
            ll now = 1;
            rep(_, it->second) now *= it->first;
            if (bit >> i & 1) {
                l *= now;
            } else {
                r *= now;
            }
            it++;
        }
        if(l == 1 or r == 1) {
            continue;
        }
        assert(gcd(l,r) == 1);
        // la = rb+1
        ll rem = l % r;
        // rem * x mod r = 1;
        // rem * x + r * y = 1  minimize x > 0
        assert(gcd(rem,r) == 1);
        ll x,y;
        extGCD(rem,r,x,y);
        ((x %= r) += r) %= r;
        if(x == 0) x += r;
        chmin(ans,l*x-1);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3660