#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

#ifdef LOCAL
#include "../ACL.hpp"
#else 
#include <atcoder/all>
#endif

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

// constexpr double EPS = 1e-10;
//constexpr double PI  = acos(-1.0);
//constexpr int INF = INT_MAX;
// constexpr int MOD = 1'000'000'007;
//inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template<class T> inline T sqr(T x) {return x*x;}

long long power(long long x, long long n, long long mod = 1LL<<60) {
    if (n == 0) return 1;
    if (n % 2 == 0) return power(x * x % mod, n/2, mod);
    else return x * power(x, n-1, mod) % mod;
}

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

void solve() {
    LL n;
    cin >> n;
    if (n == 1) {
        cout << 1 << endl;
        return;
    }
    n *= 2;
    VLL ps, psn;
    LL v = n;
    for (LL p = 2; p * p <= v; ++p) {
        if (v % p != 0) continue;
        int cnt = 0;
        while (v % p == 0) {
            ++cnt;
            v /= p;
        }
        ps.push_back(p);
        psn.push_back(cnt);
    }
    if (v > 1) {
        ps.push_back(v);
        psn.push_back(1);
    }
    int len = ps.size();
    LL ans = n / 2 - 1;
    REP(p,0,1<<len) {
        LL l = 1, r = 1;
        REP(i,0,len) {
            if (p & (1<<i)) {
                l *= power(ps[i],psn[i]);
            } else {
                r *= power(ps[i],psn[i]);
            }
        }
        LL x,y;
        LL g = extgcd(l,r,x,y);
        // cerr << l << ", " << r << ": " << x << ", " << y << ": " << g << endl;
        if (g != 1) continue;
        if (x >= 0 || y <= 0) continue;
        if (l * -x >= r * y) continue;
        // cerr << l << ", " << r << ": " << x << ", " << y << ": " << g << endl;
        chmin(ans, r * y - 1);
    }
    cout << ans << endl;
}

signed main() {
    solve();


    return 0;
}

Measured memory_bytes: 3668