require 'prime'
n = gets.strip.to_i

ans = [1,1]
divs=Prime.prime_division(n)

if divs.length==1
  p n-1
  exit
end


divs = divs.map {|pr, cnt|
  if cnt>=2
    ans[0] *= (pr**(cnt/2))
    ans[1] *= (pr**(cnt/2))
    if cnt%2==0
      nil
    else
      [pr, 1]
    end
  else
    [pr, cnt]
  end
}.compact

if divs.length.zero?
  p ans.inject(:+)-2
  exit
end

nums = divs.map{|pr,c| [pr]*c } .flatten

#p nums

result = Float::INFINITY
(nums.length).times do|c|
  temp = ans.first*nums[0..c].inject(:*) + ans.last*nums[c+1..-1].inject(1,:*)-2
#p [c, temp, ans.first*nums[0..c].inject(:*), ans.last*nums[c+1..-1].inject(1,:*)]
  result = [temp, result].min
end

p result


