require 'prime'
N = gets.to_i

primes = Prime.prime_division(N).map{|a,b|[a]*b}.flatten
result = N
(primes.size/2).downto(1) do |i|
  primes.combination(i).to_a.uniq.each do |arr|
    r = arr.inject(1) do |sum,x|
      if sum > result
        break
      end
      sum * x
    end
    r += N / r
    if r < result
      result = r
    end
  end
end
if result == N
  puts result - 1
else
  puts result - 2
end