require 'prime'

n = gets.strip.to_i
min = n - 1

unless Prime.prime?(n)
  factors = n.prime_division
  prime_table = []
  factors.each do |f|
    f[1].times.map {prime_table.push(f[0])}
  end
  (0..prime_table.length-1).each do |i|
    x = prime_table[i]
    y = 1
    (0..prime_table.length-1).each do |j|
      y *= prime_table[j] if j!=i
    end
    d = x + y - 2
    # puts "x=#{x}  y=#{y}  d=#{d}"
    min = d if d < min
  end
  puts min.to_s
else
  puts (n - 1).to_s
end