#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename T>
struct Prime{
    Prime() {}

    vector<T> divisors(const T &n) const{
        vector<T> ret;
        for(T i = 1; i*i <= n; i++){
            if(n%i == 0){
                ret.pb(i);
                if(i*i != n) ret.pb(n/i);
            }
        }
        //sort(all(ret));
        return ret;
    }

    vector<pair<T, int>> prime_factor(T n) const{
        vector<pair<T, int>> ret;
        for(T i = 2; i*i <= n; i++){
            int cnt = 0;
            while(n%i == 0) cnt++, n /= i;
            if(cnt > 0) ret.eb(i, cnt);
        }
        if(n > 1) ret.eb(n, 1);
        return ret;
    }

    bool is_prime(const T &n) const{
        if(n == 1) return false;
        for(T i = 2; i*i <= n; i++){
            if(n%i == 0) return false;
        }
        return true;
    }

    vector<bool> Eratosthenes(const int &n) const{
        vector<bool> ret(n+1, true);
        if(n >= 0) ret[0] = false;
        if(n >= 1) ret[1] = false;
        for(int i = 2; i*i <= n; i++){
            if(!ret[i]) continue;
            for(int j = 2; i*j <= n; j++) ret[i*j] = false;
        }
        return ret;
    }
};

template<typename T>
struct Euclid{
    Euclid() {}

    T gcd(const T &a, const T &b) const{
        if(b == 0) return a;
        else return gcd(b, a%b);
    }

    T lcm(const T &a, const T &b) const {return a*(b/gcd(a,b));}

    T extgcd(const T &a, const T &b, T &x, T &y) const{
        if(b == 0) {x = 1, y = 0; return a;}
        T g = extgcd(b, a%b, y, x);
        y -= (a/b)*x;
        return g;
    }

    T mod(const T &a, const T &m) const{
        T ret = a%m;
        return ret+(ret < 0? m : 0);
    }

    T modinv(const T &a, const T &m) const{ //aとmは互いに素
        T x, y;
        extgcd(a, m, x, y);
        return mod(x, m);
    }

    T floor_sum(const T &n, const T &m, T a, T b) const{ //Σ(floor((a*i+b)/m)) (0<=i<n)
        T ret = (a/m)*(n*(n-1)/2) + (b/m)*n;
        a %= m, b %= m;
        T y = (a*n+b)/m;
        if(y == 0) return ret;
        ret += floor_sum(y, a, m, a*n-(m*y-b));
        return ret;
    }

    pair<T, T> Chinese_rem(const T &a1, const T &m1, const T &a2, const T &m2) const{
        T x, y, g = extgcd(m1, m2, x, y);
        if((a2-a1)%g != 0) return make_pair(0, -1);
        T m = m1*(m2/g);
        T tmp = mod(x*((a2-a1)/g), m2/g);
        T a = (m1*tmp+a1) % m;
        return make_pair(a, m);
    }

    T Garner(vector<T> a, vector<T> m, const T &M) const{
        m.pb(M);
        vector<T> coeffs(sz(m), 1);
        vector<T> constants(sz(m), 0);
        rep(k, sz(a)){
            T t = mod((a[k]-constants[k]) * modinv(coeffs[k], m[k]), m[k]);
            rep2(i, k+1, sz(m)-1){
                constants[i] += t*coeffs[i], constants[i] %= m[i];
                coeffs[i] *= m[k], coeffs[i] %= m[i];
            }
        }
        return constants.back();
    }
};

int main(){
    ll N;
    cin >> N;
    Prime<ll> P; Euclid<ll> E;
    vector<pli> v = P.prime_factor(2*N);
    int n = sz(v);
    ll ans = INF;
    rep(i, 1<<n){
        ll a = 1, b = 1;
        rep(j, n){
            if(i&(1<<j)){
                rep(k, v[j].second) a *= v[j].first;
            }
            else{
                rep(k, v[j].second) b *= v[j].first;
            }
        }
        ll x, y;
        E.extgcd(a, b, x, y);
        if(y < 0){
            ll k = (abs(y)-1)/a;
            x -= k*b, y += k*a;
        }
        else{
            ll k = (y/a)+1;
            x += k*b, y -= k*a;
        }
        chmin(ans, -b*y);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3592