require 'prime'

n = gets.chomp.to_i

factors = Prime.prime_division(n)

result = n-2
1.upto(Math.sqrt(n)) do |ni|
  a = n / ni
  b = n % ni
  next if b != 0
  result = a+ni-2 if (a+ni-2) < result
end

puts result
