n = gets.to_i
m = n
divs = []
while m % 2 == 0 do
  divs.push(2)
  m /= 2
end
d = 3
while d * d <= m do
  if m % d == 0
    divs.push(d)
    m /= d
  else
    d += 2
  end
end
divs.push(m) if m > 1
min_move = n - 1
for i in 0...(2 ** divs.size) do
  a = 1
  b = 1
  for j in 0...divs.size do
    if (i >> j) & 1 == 0
      a *= divs[j]
    else
      b *= divs[j]
    end
  end
  move = (a - 1) + (b - 1)
  min_move = move if min_move > move
end
puts min_move
