require "prime"
N = gets.chomp.to_i
f = N.prime_division.map do |n, k|
  [n] * k
end.flatten.sort

c = [N-1]
0.upto(f.size-2) do |i|
  c << (f[i...f.size-1].inject(&:*)-1) + (f[0...i] + [f[f.size-1]]).inject(&:*)-1
end
puts c.min
