import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


Int mod(Int)(Int a, Int m) {
  if ((a %= m) < 0) a += m; return a;
}
Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; }
Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; }

Int gojo(Int)(Int a, Int b, out Int x, out Int y) {
  if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; }
  x = 1; y = 0; return a;
}

Tuple!(Int, "b", Int, "m") modSol(Int)(Int b0, Int m0, Int b1, Int m1) {
  Int b, m, x, y;
  const g = gojo(m0, m1, x, y);
  if ((b1 - b0) % g == 0) {
    b = b0 + m0 * ((x * (((b1 - b0) / g) % (m1 / g))) % (m1 / g));
    m = m0 * (m1 / g);
    if ((b %= m) < 0) {
      b += m;
    }
  }
  return Tuple!(Int, "b", Int, "m")(b, m);
}


void main() {
  try {
    for (; ; ) {
      const N = readLong();
      
      long n = 2 * N;
      long[] pes;
      for (long p = 2; p^^2 <= n; ++p) {
        if (n % p == 0) {
          long e;
          do {
            ++e;
            n /= p;
          } while (n % p == 0);
          pes ~= p^^e;
        }
      }
      if (n > 1) {
        pes ~= n;
      }
      const pesLen = cast(int)(pes.length);
      debug {
        writeln("pes = ", pes);
      }
      
      BigInt ans = long.max;
      void dfs(int i, BigInt b, BigInt m) {
        if (i == pesLen) {
          debug {
            writeln(b, " ", m);
            foreach (j; 0 .. pesLen) {
              writeln("  ", pes[j], " ", b % pes[j]);
            }
          }
          chmin(ans, (b == 0) ? m : b);
        } else {
          foreach (r; [0, pes[i] - 1]) {
            const res = modSol(b, m, BigInt(r), BigInt(pes[i]));
            dfs(i + 1, res.b, res.m);
          }
        }
      }
      dfs(0, BigInt(0), BigInt(1));
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
