
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

n=int(input())
F = [1]
for i, j in factorization(n):
    for _ in range(j):
        F.append(i)
import itertools
ans = 9999999999999
for flags in itertools.product([0,1], repeat=len(F)):
    a = 1
    b = 1
    for i in range(len(F)):
        if flags[i]:
            a *= F[i]
        else:
            b *= F[i]
    tmp = (a-1) + (b-1)
    if tmp < ans:
        ans = tmp
print(ans)