require 'prime'

N = gets.chomp.to_i
divs = Prime.prime_division(N).to_a
primes = divs.flat_map {|v, n| Array.new(n, v)}

min = Float::INFINITY
combination_size_max = divs.map(&:last).reduce(:+) / 2

if combination_size_max.zero?
  puts(N - 1)
  exit
end

cache = {}

(1..combination_size_max).each do |combination_size|
  primes.combination(combination_size).each do |combination|
    a = combination.reduce(:*)
    next if cache[a]
    b = N / a
    m = a.pred + b.pred
    cache[a] = m
    min = m < min ? m : min
  end
end

puts min
