n = int(eval(input()))

dv = float('inf')

def make_divisors(n):

    divisors = []

    for i in range(1, int(n**0.5)+1):

        if n % i == 0:

            divisors.append(i)

            if i != n // i:

                divisors.append(n//i)

    divisors.sort()

    return divisors



div = make_divisors(n)

# print(make_divisors(n), len(make_divisors(n)))

# print(make_divisors(n)[169//2])

l = len(make_divisors(n))

# print(div)

if l % 2 == 0:

    for i in range(l//2):

        if dv > (div[i]-1) + (div[l-i-1]-1):

            dv = (div[i]-1) + (div[l-i-1]-1)

else:

    for i in range((l//2)+1):

        if dv > (div[i]-1) + (div[l-i-1]-1):

            dv = (div[i]-1) + (div[l-i-1]-1)



print(dv)
N = int(eval(input()))



def make_divisors(n):

    divisors = []

    for i in range(1, int(n**0.5)+1):

        if n % i == 0:

            divisors.append(i)

            if i != n // i:

                divisors.append(n//i)

    divisors.sort()

    return divisors



div = make_divisors(N)

if len(div) % 2 == 0:

    print(((div[len(div) // 2] - 1) + (div[len(div) // 2 - 1] - 1)))

else:

    print(((div[len(div) // 2] - 1)*2))
n = int(eval(input()))
dv = float("inf")


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)
    divisors.sort()
    return divisors


div = make_divisors(n)
# print(make_divisors(n), len(make_divisors(n)))
# print(make_divisors(n)[169//2])
l = len(make_divisors(n))
# print(div)
if l % 2 == 0:
    for i in range(l // 2):
        if dv > (div[i] - 1) + (div[l - i - 1] - 1):
            dv = (div[i] - 1) + (div[l - i - 1] - 1)
else:
    for i in range((l // 2) + 1):
        if dv > (div[i] - 1) + (div[l - i - 1] - 1):
            dv = (div[i] - 1) + (div[l - i - 1] - 1)
print(dv)

N = int(eval(input()))


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)
    divisors.sort()
    return divisors


div = make_divisors(N)
if len(div) % 2 == 0:
    print(((div[len(div) // 2] - 1) + (div[len(div) // 2 - 1] - 1)))
else:
    print(((div[len(div) // 2] - 1) * 2))

-n = int(eval(input())) -dv = float("inf") +N = int(eval(input())) -div = make_divisors(n) -# print(make_divisors(n), len(make_divisors(n))) -# print(make_divisors(n)[169//2]) -l = len(make_divisors(n)) -# print(div) -if l % 2 == 0: -    for i in range(l // 2): -        if dv > (div[i] - 1) + (div[l - i - 1] - 1): -            dv = (div[i] - 1) + (div[l - i - 1] - 1) +div = make_divisors(N) +if len(div) % 2 == 0: +    print(((div[len(div) // 2] - 1) + (div[len(div) // 2 - 1] - 1))) -    for i in range((l // 2) + 1): -        if dv > (div[i] - 1) + (div[l - i - 1] - 1): -            dv = (div[i] - 1) + (div[l - i - 1] - 1) -print(dv) +    print(((div[len(div) // 2] - 1) * 2))