require 'prime'
N = gets.chomp.to_i
cnt = 0

if N.prime?
  puts N - 1
  exit
end

NN = N / 2
tmpp = N
(2..(NN+1)).each do |num|
  tmp2 = N % num
  if tmp2 == 0
    tmp = N / num
	a = tmp + num
	if tmpp > a
      tmpp = a
    end
  end
end
puts tmpp - 2