Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache


def factorize(n):
    f = []
    tmp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        while tmp % i == 0:
            f.append(i)
            tmp //= i
    if tmp != 1:
        f.append(tmp)
    if not f:
        f.append(n)
    return f


def distance(s):
    a, b = 1, 1
    for i, si in enumerate(s):
        if si == "0":
            a *= factors[i]
        else:
            b *= factors[i]
    return a + b - 2


@lru_cache()
def kp(i, s):
    if i == 0:
        return 0, s
    else:
        if distance("0" + s) < distance("1" + s):
            return kp(i - 1, "0" + s)
        else:
            return kp(i - 1, "1" + s)


def sa(f):
    d = 999999999999
    for i in range(2 ** len(f)):
        d = min(d, distance("{}{}".format("0" * (len(f) - 1), bin(i)[2:])[-len(f):]))
    return d


N = int(input())
factors = factorize(N)
print(sa(factors))

Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w // 2):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
        if ans < n ** 0.5 * 2:
            break
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w // 2):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
        if ans < n ** 0.5 * 2:
            break
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w // 2):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
        if ans < n ** 0.5 * 1.5:
            break
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w // 2):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
        if ans < n ** 0.5 * 1.1:
            break
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w // 2):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    ans = 1000000000000
    for i in range(1, 2 ** w):
        f2 = [1, 1]
        for j in range(w):
            f2[i & (2 ** j) == 0] *= factor[j]
        ans = min(ans, sum(f2) - 2)
    print(ans)

Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
w = len(factor)
if w == 0:
    factor.append(n)
    w = 1
if w == 1:
    print(n - 1)
else:
    # bit = [0] * w
    ans = 1000000000000
    for i in range(1, 2 ** w):
        a, b = 1, 1
        for j in range(w):
            # bit[j] = i & (2 ** j)
            if i & (2 ** j) == 0:
                a *= factor[j]
            else:
                b *= factor[j]
        if a + b - 2 < ans:
            ans = a + b - 2
        # print(i, bit, a, b, ans)
    print(ans)

Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
if len(factor) == 0:
    factor.append(n)
if len(factor) == 1:
    print(factor[0] - 1)
else:
    ans = float("inf")
    for i in range(1, 2 ** len(factor)):
        a, b = 1, 1
        for j in range(len(factor)):
            if i & (2 ** j) == 0:
                a *= factor[j]
            else:
                b *= factor[j]
        if a + b - 2 < ans:
            ans = a + b - 2
    print(ans)


Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
factor = []
tmp = n
for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
    while tmp % i == 0:
        factor.append(i)
        tmp //= i
if tmp != 1:
    factor.append(tmp)
if len(factor) == 0:
    factor.append(n)
if len(factor) == 1:
    print(factor[0] - 1)
else:
    ans = float("inf")
    for i in range(1, 2 ** len(factor)):
        a, b = 1, 1
        for j in range(len(factor)):
            if i & (2 ** j) == 0:
                a *= factor[j]
            else:
                b *= factor[j]
        if a + b - 2 < ans:
            ans = a + b - 2
    print(ans)

Fixed submission:
n = int(input())
ans = 1000000000000
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        a = i
        b = n // i
        ans = min(ans, a + b - 2)
print(ans)