require "prime"
n = gets.chomp.to_i
pri = Prime.prime_division(n)
arr = []
pri.each do |pr|
	arr << Array.new(pr[1], pr[0])
end
arr.flatten!

min = n + 5
[false, true].repeated_permutation(arr.length) do |bits|
	a = 1 
	b = 1 
	bits.each_with_index do |bit, i|
		if bit 
			a *= arr[i]
		else
			b *= arr[i]
		end
	end
	#puts "a:#{a} b:#{b}"
	min = a + b - 2 if min > a + b - 2 
end
puts min
