require 'prime'
N = gets.chomp.to_i
if N.prime?
	puts N-1
else
  factors = N.prime_division
  M = Math.sqrt(N).floor
  if M <= factors.last[0]
	while M <= factors.last[0] do
  		break if N % M ==0
  		M += 1
	end
  else
    while M >= 1 do
      break if N % M ==0
  	  M -= 1
	end
  end
  puts (M + N/M -  2)
end