lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end struct Int def prime_factor : Array(Tuple(self, Int32)) result = [] of Tuple(self, Int32) n = self typeof(self).new(2).upto(Math.sqrt(self).ceil) do |x| count = 0 while n % x == 0 n //= x count += 1 end result << {x, count} if count > 0 end result << {n, 1} if n != 1 result end def divisors : Array(self) result = [] of self each_divisor do |d| result << d end result end def each_divisor(&) tmp = [] of self typeof(self).new(1).upto(self) do |x| break if x * x > self if self % x == 0 yield x tmp << x end end (0...tmp.size).reverse_each do |i| yield self // tmp[i] if tmp[i] * tmp[i] < self end end end n = read_line.to_i64 ans = n + 1 (2i64..Math.sqrt(n).to_i64).each do |r| val = 1i64 while val <= n ans = {ans, r}.min if n % val == 0 && n // val < r val = val * r + 1 end end n.each_divisor do |a| b = n // a - 1 ans = {ans, b}.min if a < b end puts ans