class Array
    def subtract_once(values)
        counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
        reject { |e| counts[e] -= 1 unless counts[e].zero? }
    end
end

def cc144
    n = gets.chomp.to_i

    m = Math.sqrt(n).to_i
    if m * m == n
        puts (m-1) * 2
        exit
    end

    require 'Prime'
    if Prime.prime?(n)
        puts n-1
        exit
    end

    x = []
    Prime.prime_division(n).each do |m|
        m[1].times do
            x.push(m[0])
        end
    end

    mi = 1000000000001
    a = []
    b = []
    (0..x.length).inject([]) do |ret, m|
        ret.push(*x.combination(m))
    end.each do |y|
        z = x.subtract_once(y)
        next unless y.length+z.length == x.length

        yy = y.inject(1) {|sum, m| sum *= m}
        zz = z.inject(1) {|sum, m| sum *= m}
        cm = (yy - zz).abs
        next if cm == 0

        mi = [mi, cm].min
        if cm == mi
            a = yy
            b = zz
        end
    end

    puts (a-1) + (b-1)
end

cc144