require 'prime'

N = gets.chomp.to_i
divs = Prime.prime_division(N).to_a

commons = []
primes = []
divs.each do |v, n|
  primes.push(v) if n.odd?
  next if n < 2
  commons.push(v ** (n / 2))
end

if commons.empty? && primes.empty?
  puts(N - 1)
  exit
end

cache = {}
min = Float::INFINITY
cp = commons.reduce(:*) || 1
min_a, min_b = nil, nil

(1..([primes.size/2, 1].max)).each do |combination_size|
  primes.combination(combination_size).each do |combination|
    a = combination.reduce(:*) * cp
    next if cache[a]
    cache[a] = true
    b = N / a
    c = a + b
    if c < min
      min_a = a
      min_b = b
      min = c
    end
  end
end

unless min_a && min_b
  puts(N - 1)
  exit
end

puts(min_a + min_b - 2)
