require "prime"

n = gets.to_i
pf = Prime.instance.prime_division(n * 2)
pf.map! { |a, b| a ** b }

def each_subset(ary, &block)
  1.upto(ary.size - 1) { |n| ary.combination(n, &block) }
end

candidates = []
each_subset(pf) { |ary|
  a = ary.inject(1, :*)
  b = n * 2 / a

  # find a.x - b.y = 1
  s, t, x, x0, y, y0 = a, b, 1, 0, 0, 1
	until t.zero?
		s, (q, t) = t, s.divmod(t)
		x, x0 = x0, x - q * x0
		y, y0 = y0, y - q * y0
	end

  candidates << a * x << b * y
}

p candidates.map(&:abs).min
