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