/* lib.cpp */

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <queue>
#include <algorithm>
#include <iostream>
#include <iomanip>

#define overload4(_1,_2,_3,_4,name,...) name
#define overload3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=(n)-1;i>=0;i--)
#define rrep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep3(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define rrep4(i,a,b,c) for(ll i=a+(b-a-1)/c*c;i>=a;i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each(i,...) for(auto&& i:__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define elif else if
#define unless(a) if(!(a))
#define mp make_pair
#define mt make_tuple

using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pll = pair<ll, ll>;
using graph = vector<vector<int>>;

const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x1fffffffffff;
const ll INF = 0x3fffffff;
const ll MOD = 1e9 + 7;
const ll MODD = 998244353;
const ld EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int edx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int edy[8] = {0, 1, 1, 1, 0, -1, -1, -1};


template<class T> inline T min(const vector<T> &v){ return *min_element(all(v)); }
inline char min(const string &v){ return *min_element(all(v)); }
template<class T> inline T max(const vector<T> &v){ return *max_element(all(v)); }
inline char max(const string &v){ return *max_element(all(v)); }
template <class T> inline void chmin(T &a, T b) { a = min(a, b); }
template <class T> inline void chmax(T &a, T b) { a = max(a, b); }


ll modpow(ll a, ll b, ll p = MOD) { ll ret = 1; while (b != 0) { if ((b & 1) == 1) ret *= a; a *= a; if (a >= p) a %= p; if (ret >= p) ret %= p; b >>= 1; } return ret; }
int digit(ll x, ll base = 10) { int ret = 0; while (x) { x /= base; ret++; } return ret; }
int sumOfDigit(ll x, ll base = 10) { int ret = 0; while(x) { ret += x % base; x /= base; } return ret; }
ll factrial(int n) { ll ret = 1; for(int i = 1;i <= n;i++) { ret *= i; } return ret; }
ll gcd(ll a, ll b) { while(b != 0) { a %= b; swap(a, b); } return abs(a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modinv(ll a, ll p = MOD) { ll b = p, u = 1, v = 0; while(b) { ll tmp = a / b; a -= tmp * b; u -= tmp * v; swap(a, b); swap(u, v); } u %= p; if (u < 0) u += p; return u; }
bool isprime(ull x) { if(x <= 1) return false; for(int i = 2;i * i <= x;i++) if(x % i == 0) return false; return true; }
map<ll, ll> factor(ull x){ map<ll, ll> ret; for(ll i = 2; i * i <= x; i++) while(x % i == 0){ ret[i]++; x /= i; } if(x != 1) ret[x]++; return ret; }
set<ll> divisor(ull x){ set<ll> ret; for(ll i = 1; i * i <= x; i++) if(x % i == 0){ ret.insert(i); ret.insert(x / i); } return ret; }
ll minDivisor(ull x, ll n = 2) { if(x <= 1) return 1; for(int i = n;i * i <= x;i++) if(x % i == 0) return i; return x; }
ll countDivisor(ull x) { map<ll, ll> m = factor(x); ll ret = 1; for(auto i : m) { ret *= i.second + 1; } return ret; }

class disjoint_set
{
    vector<int> _parent, _size;

public:
    disjoint_set(int n) : _parent(n), _size(n) {
        for (int i = 0; i < n; i++) {
            _parent[i] = i;
            _size[i] = 1;
        }
    }
    int root(int x) {
        if (_parent[x] == x) return x;
        return _parent[x] = root(_parent[x]);
    }
    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        _parent[rx] = ry;
        _size[ry] += _size[rx];
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return _size[root(x)]; }
};

void init() { cin.tie(nullptr); cout << fixed << setprecision(15); }

#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using bint = cpp_int;

int solve() {
    ll n;
    cin >> n;
    map<ll, ll> m = factor(n);
    ll ans = LINF;

    ll h = m.size();
    vector<ll> f(h);
    ll in = 0;
    for(auto it = m.begin(); it != m.end(); it++) {
        f[in] = modpow(it->first, it->second, LINF);
        in++;
    }
    ll p = modpow(2, h, LINF);
    rep(i, p) {
        ll x = 1;
        ll ii = i;
        rep(j, h) {
            if(ii % 2 == 1) x *= f[j];
            ii /= 2;
        }
        ll mm = (modinv(x, n / x) * (-1)) % (n / x);
        if(mm <= 0) mm += n / x;
        if(((bint)mm * (x * mm + 1) / 2) % (n / x) == 0) chmin(ans, x * mm);
    }
    cout << ans << endl;
    return 0;
}

int main() { init(); solve(); return 0; }


Measured memory_bytes: 3652