Problem:
 Score : 300 points    Problem Statement Takahashi is standing on a multiplication table with infinitely many rows and columns.  The square (i,j) contains the integer i \times j . Initially, Takahashi is standing at (1,1) .  In one move, he can move from (i,j) to either (i+1,j) or (i,j+1) .  Given an integer N , find the minimum number of moves needed to reach a square that contains N .      Constraints  2 \leq N \leq 10^{12}  N is an integer.         Input Input is given from Standard Input in the following format:  N       Output Print the minimum number of moves needed to reach a square that contains the integer N .        Sample Input 1 10      Sample Output 1 5  (2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.       Sample Input 2 50      Sample Output 2 13  (5, 10) can be reached in 13 moves.       Sample Input 3 10000000019      Sample Output 3 10000000018  Both input and output may be enormous.

Solution (Python):
import numpy as np

def solve():
    N = int(input())
    
    r = 1e12
    for i in range(1, int(np.sqrt(N)) + 1):
        if N % i == 0:
            r = min(r, (N / i - 1) + (i - 1))
            
    
    print(int(r))

solve()

Solution (Python):
import math
N = int(input())
n = int(math.sqrt(N))
divisor = [i for i in range(1, n + 1) if N % i == 0]
XY = [div + N // div - 2 for div in divisor]
print(min(XY))

Solution (Python):
N = int(input())

min = float('inf')

times = int(N**0.5)

for i in range(1, times+1, 1):

    if N%i==0:
        j = N//i
    else:
        continue
    temp = i+j-2
    if min > temp:
        min = temp

print(min)

Solution (Python):
import math

N = int(input())

hoji = float("inf")
for i in range(1,int(math.sqrt(N))+1):
    if N%i == 0 and (i-1)+((N//i)-1) < hoji:
        hoji = (i-1)+((N//i)-1)

print(hoji)

Solution (Python):
import math

N = int(input())

hoji = float("inf")
for i in range(1,int(math.sqrt(N))+1):
    if N%i == 0 and (i-1)+((N//i)-1) < hoji:
        hoji = (i-1)+((N//i)-1)

print(hoji)

Solution (Python):
import math
n = int(input())
r = list()
for a in range(1,int(math.sqrt(n))+1):
    if n % a == 0:
        b = n//a
        r.append(a+b-2)
print(min(r))

Solution (Python):
import math
n = int(input())
r = list()
for a in range(1,int(math.sqrt(10**12)+1)):
    if n % a == 0:
        b = n//a
        r.append(a+b-2)
print(min(r))

Solution (Python):
N=int(input())
t=0
i=int(N**0.5)+1
while i>=2:
    if N%i==0:
        t=i
        break
    else:
        i-=1
        t=N
if t==N:
    print(t-1)
else:
    print(t-1+N//t-1)

Solution (Python):
import numpy as np
n = int(input())

x,y=0,0
for i in range(1, int(np.sqrt(n)) + 1):
    if n % i == 0:
        x = i
        y = n // x

print(x+y-2)

Solution (Python):
n = int(input())
 
l1 = []
for i in range(1, int(n ** 0.5) + 1):
  q, mod = divmod(n , i)
  if mod == 0:
    l1.append(q + i -2)
print(min(l1))

Solution (Python):
import sys
import math

def main():
    N = int(input())

    for a in reversed(range(1, int(math.sqrt(N))+1)):
        if N % a == 0:
            print(int(a + N/a - 2))
            sys.exit()

main()

Solution (Python):
import sys
import math

def main():
    N = int(input())

    for a in reversed(range(1, int(math.sqrt(N))+1)):
        if N % a == 0:
            print(int(a + N/a - 2))
            sys.exit()

main()

Solution (Python):
N=int(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)

    
    return divisors

divisors=make_divisors(N)
l=[]
for i in divisors:
    tmp=N//i
    l.append(i-1+tmp-1)

print(min(l))

Solution (Python):
import math
N = int(input())
ans = float("inf")

for i in range(1,int(math.sqrt(N))+1):
    if N%i == 0:
        ans = min(ans,i+N//i-2)
print(int(ans))

Solution (Python):
N = int(input())
i = 1
ans = float("inf")
while i * i <= N:
    if N % i == 0:
        ans = min(ans, i + N // i - 2)
    i += 1
print(ans)

Solution (Python):
n = int(input())
a=[]
for i in range(1,int(n**0.5)+1):
    if  n%i == 0:
        a.append([i,n//i])
b = 10**14
for i in range(len(a)):
    b = min(b,(a[i][0]-1)+(a[i][1]-1))
print(b)

Solution (Python):
def md(n):
    d=[]
    da=d.append
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            da(i)
            if i!=n//i:da(n//i)
    d.sort()
    return d
n = int(input())
t=md(n)
u=len(t)
midopen = 0
mini = 999999999999999

if u%2==1:midopen=u//2
else:midopen=u//2+1
for i in range(min(u-1,midopen+1)):
    x = t[i]
    y = n//x
    
    mini = min(mini,x+y-2)
print(mini)

Solution (Python):
N=int(input())
a=int(N**0.5)+1
answer = float('inf')
for i in range(1,a):
  if N%i==0:
    answer = min(answer,i+(N//i-2))
print(answer)

Solution (Python):
N=int(input())
a=int(N**0.5)+1
answer = float('inf')
for i in range(1,a):
  if N%i==0:
    answer = min(answer,i+(N//i-2))
print(answer)

Solution (Python):
N = int(input())

ret = float("inf")
for i in range(1, int(N**0.5) + 1):
  if N % i == 0:
    ret = min(N // i + i - 2, ret)
    
print(ret)

Solution (Python):
import math
a = int(input())
b = int(math.sqrt(a))
for i in range(b):
    x = a/b
    if x.is_integer():
        break
    else:
        b -= 1
print(int(x + b -2))

Solution (Python):
from math import sqrt
from math import floor
n=int(input())
c=floor(sqrt(n))
t=[]
for i in range(1,c+1):
  if(n%i==0):
    d=n//i
    t.append(i)
    t.append(d)
print(t[-1]+t[-2]-2)

Solution (Python):
import math
a = int(input())
root = int(math.sqrt(a))+1
left = 1
for i in range(2, root):
    if(a%i==0):
        left = i
total = left+(a//left)
print(total-2)

Solution (Python):
import math

n = int(input().strip())
rep = []
for a in range(1, math.floor(n ** 0.5) + 1, 1):
    m = n % a
    if m == 0:
        rep.append(a + n // a)
print(min(rep) - 2)

Solution (Python):
n = int(input())

ans = 1000000000000

a = 1
while a * a <= n:
  if n % a != 0:
    a += 1
    continue
  b = n // a
  ans = min(ans, a + b - 2)
  a += 1

print(ans)

Solution (Python):
N = int(input())
min_result = 10**12
for n in range(1,int(N**0.5)+1):
    if N%n == 0:
        result = n + int(N/n) - 2
        if min_result > result:
            min_result = result
print(min_result)

Solution (Python):
import math

n = int(input())

ans = 10**12
for i in range(1, int(n ** 0.5) + 1):
    if n % i != 0:
        continue
    move = i + int(n / i) - 2
    if move < ans:
        ans = move
print(ans)

Solution (Python):
n = int(input())

minimum = 10**12

a = 1

while a <= n**(1/2):
    b = n / a
    if b.is_integer():
        if a + b - 2 < minimum:
            minimum = a + b - 2
    
    else:
        a += 1
        continue
    
    a += 1

print(int(minimum))

Solution (Python):
n = int(input())
a = int(n**0.5)

while n % a != 0:
  a = a-1
else:
  print(a+n//a-2)

Solution (Python):
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)

    
    return divisors

N=int(input())
divisors = make_divisors(N)

distance = [ i - 1 + int(N / i) - 1 for i in divisors]
print(min(distance))

Solution (Python):
import math
n=int(input())
a=math.sqrt(n)
b=round(a)
for i in range(b,0,-1):
    if n%i==0:
        print(n//i+i-2)
        break

Solution (Python):
N = int(input())

divisors = []
for x in range(1, int(N**0.5) + 1):
    if N % x == 0:
        divisors.append(x)

sums = []
for x in divisors:
    i, j = x, N // x
    sums.append(i + j)

print(min(sums) - 2)

Solution (Python):
import math
N = int(input())

ans = 1e15
i = 0
while i < math.sqrt(N): 
  i += 1
  if N%i > 0:
    continue
  a = i
  b = N // i
  ans = min(ans, a+b-2)

  
print(ans)

Solution (Python):
from math import sqrt

N = int(input())

for i in range(int(sqrt(N)) + 1, -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        break

Solution (Python):
n=int(input())
ab = []
for i in range(1,int(n**(1/2))+1):
    if n%i == 0:
        ab.append((n//i,i))
ans=10**12
for i in range(len(ab)):
    a,b = ab[i]
    ans=min(a+b-2,ans)
print(ans)

Solution (Python):
import sys
import math
n=int(input())

b=n
a=[]
N=int(math.sqrt(n))

for i in range(1,N+1):
 q, mod=divmod(n,i)
 a=q+i-2

 if(mod==0 and a<b):
     b=a

print(b)

Solution (Python):
N = int(input())
li = list()
n = N + 1
for i in range(1, int(N**0.5+1)):
	if N % i == 0:
		li.append([i, int(N/i)])
for x in li:
	tmp = x[0] + x[1]
	if tmp <= n:
		n = tmp
ans = n - 2
print(ans)

Solution (Python):
n=int(input())
sqrter=int(n**0.5)
i=sqrter
while(i>=1):
    if(n%i==0):
        print(i+n//i-2)
        break
    i-=1

Solution (Python):
n = int(input())

def min_walk(n):
    out = []
    for a in range(1, int(n**0.5)+1):
        if n % a == 0:
            out.append(a + (n//a) - 2)

    return min(out)


print(min_walk(n))

Solution (Python):
from math import sqrt
from sys import exit

N = int(input())

for i in range(int(sqrt(N)) + 1, -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        exit()

Solution (Python):
import numpy as np
n=int(input())
ans=n
for i in range(1,int(np.sqrt(n))+1):
    if n%i==0:
        ans=np.min((ans,i+int(n/i)-2))
print(ans)

Solution (Python):
N = int(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)

    
    return divisors

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

Solution (Python):
N=int(input())
a=1
b=N
mx=int(1+N**0.5)
for i in range(1,mx):
    if N%i==0:
        if (a+b)>(i+N//i):
            a=i
            b=N//i
print(a+b-2)

Solution (Python):
n = int(input())
nr = int(n ** 0.5) + 1
for i in range(nr,0,-1):
    if n % i == 0:
        j = n // i
        break
print(i + j -2)

Solution (Python):
import math
n = int(input())

ans = 0
for i in range(math.ceil(math.sqrt(n) + 1))[:0:-1]:
    if 0 == n % i:
        ans = i
        break
ans_partner = n // ans
print(ans + ans_partner - 2)

Solution (Python):
N = int(input())
minimal = N - 1
for i in range(1,int(N**0.5)+1):
    if N%i == 0:
        minimal = min(minimal, i+N/i-2)
print(int(minimal))

Solution (Python):
n = int(input())
th = int(n ** 0.5) + 1

for i in range(th, 0, -1):
  if n % i == 0:
    j = n // i
    break

print(i + j - 2)

Solution (Python):
N = int(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(reverse=True)
    return divisors
div = make_divisors(N)
l = len(div) // 2
a = div[l]
b = N // a
ans = (a - 1) + (b - 1)
print(ans)

Solution (Python):
N = int(input())
rslt = 1+N
rt_N = int(N**0.5)
for i in range(rt_N, 0, -1):
    if N%i == 0:
        rslt = i + int(N/i) -2
        break;
print(rslt)

Solution (Python):
import math
if __name__=='__main__':
    n=int(input())
    oldn=10**20
    for i in range(int(math.sqrt(n))+1):
        if n%(i+1)==0 and oldn>=(((i+1)+n//(i+1))-2):
            oldn=(((i+1)+n//(i+1))-2)
        elif n%(i+1)==0 and oldn<(((i+1)+n//(i+1))-2):
            break
    print(oldn)

Solution (Python):
N = int(input())

for i in reversed(range(1, int(N**0.5)+1)):
    j = int(N/i)
    if i*j == N:
        print(i-1+j-1)
        break

Solution (Python):
import math

n = int(input())
p = math.floor(math.sqrt(n))
while not (n/p).is_integer():
	p -= 1
q = int(n/p)
print(p-1 +  q-1)

Solution (Python):
N = int(input())
x = int( N**0.5 )

for i in range(x, 0, -1):
    if N % i == 0:
        break

print(i + (N // i) - 2)

Solution (Python):
import math

n = int(input())

ans = float('inf')
for i in range(1, math.ceil(n**(1/2))+1):
    if n%i == 0:
        a = i
        b = n // i
        d = a-1 + b-1
        ans = min(ans, d)

print(ans)

Solution (Python):
N=int(input())
from itertools import combinations

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

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

import math 
n=int(math.sqrt(N))
k=n
divisors = []
if (n ** 2) == N:
    k = 2 * (n-1)
else:
    divisors = make_divisors(N)
    center = (len(divisors) // 2) - 1
    k = divisors[center] - 1 + divisors[center+1] - 1

print(k)

Solution (Python):
import math
N=int(input())
p=math.floor(math.sqrt(N))
while N % p > 0:
    p-=1
q=N//p
print(p+q-2)

Solution (Python):
N = int(input())


num=int(N**(1/2)+1)
count=N+1-2



for i in range(1,num):
    if N%i==0:
       count=min(count,i+N//i -2)
print(count)

Solution (Python):
n = int(input())

res = n-1
for i in range(1, int(n**(1/2))+1):
    if n % i == 0:
        res = n//i - 1 + n//(n//i) - 1
print(res)

Solution (Python):

from math import sqrt,floor
n = int(input())

x = floor(sqrt(n))
for i in range(x,-1,-1):
    if n%i==0:
        print(i+n//i-2)
        exit()

Solution (Python):
N = int(input())

root = int(N ** (1/2))

ans = N + 1 - 2

for i in range(1, root+1):
    if (N % i == 0):
        ans = min(ans, i + N // i - 2)
print(ans)

Solution (Python):
N=int(input())
D=[]
i=1
while(i*i<=N):
    if N%i==0:
        D.append(i)
        D.append(N//i)
    i+=1
ans=N-1
for i in D:
    j=N//i
    ans=min(ans,i+j-2)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    for i in range(int(n**.5),0,-1):
        q,r=divmod(n,i)
        if(r==0):
            print(i+q-2)
            return
resolve()

Solution (Python):
n=int(input())
for i in range(int(n**0.5),0,-1):
    if n%i==0:
        print(i+n//i-2)
        exit()

Solution (Python):
from math import sqrt, ceil

n = int(input())

for i in range(ceil(sqrt(n)), 0, -1):
    if n % i == 0:
        print(i + n // i - 2)
        break

Solution (Python):
N = int(input())
result = N+1;
for i in range(1,int(N**0.5)+1):
    if N%i == 0:
        a = N//i
        b = a+i
        if b < result:
            result = b
print(result-2)

Solution (Python):



n = int(input());



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

    
    return divisors
    
m = make_divisors(n)


count = 0;
for i in range(0,len(m),2):
    
    
        
    tmp =  (m[i]-1) + (m[i+1]-1);
    
    if tmp < count or count == 0:
        count = tmp;
    
        
print(count);

Solution (Python):
n=int(input())
mx=10**15
for i in range(1,int(n**.5+1)):
    if n%i==0 and n//i+i-2<mx:
        mx=n//i+i-2
print(mx)

Solution (Python):
import math
N = int(input())
root_N = int(math.sqrt(N))
ans = 10 ** 12

for i in range(1, root_N + 1):
    if N % i == 0:
        mov = (N // i) + i - 2
        ans = min(ans, mov)

print(ans)

Solution (Python):
N = int(input()) 

tmp = int(N**0.5)+1
ans = N
for i in range(1, tmp):
    if N%i==0:
        ans = min(ans, i+N//i-2)
    
print(ans)

Solution (Python):
import math

def main():
    N = int(input())
    min_steps = N
    for i in range(1, math.floor(math.sqrt(N))+1):
        if N % i == 0:
            a, b = i, N//i
            min_steps = min(min_steps, a-1+b-1)
    print(min_steps)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
i = 1
res = n
while i * i <= n:
  if n % i == 0:
    res = min(res, i + n//i - 2)
  i += 1
print(res)

Solution (Python):
N = int(input())
N_sqrt = int(N**(1/2))
for i in range(1,N_sqrt+1):
    if N%i == 0:
        num = i
ans = num+N//num-2
print(ans)

Solution (Python):
import numpy as np

N = int(input())

for i in range(1, int(np.sqrt(N)) + 1):
    if N % i == 0:
        ans = (i - 1) + ((N / i) - 1)

print(int(ans))

Solution (Python):
import numpy as np
N = int(input())

for i in range(int(np.sqrt(N)), 0, -1):
    if N % i == 0:
        break

a1, a2 = i, N//i

print(a1 + a2 - 2)

Solution (Python):
N = int(input())
num_list = [(i-1) + (N//i-1) for i in range(1, int(N**0.5+1)) if N%i == 0]
print(min(num_list))

Solution (Python):
N = int(input())
print(min([
    (i-1) + (N//i-1)
    for i in range(1, int(N**0.5+1))
    if N%i == 0
]))

Solution (Python):
import math

n = int(input())
upper = int(math.sqrt(n))

mx = 1
for i in range(1, upper + 1):
    if n % i == 0:
        mx = i

h = mx
v = n // mx

move = h - 1 + v - 1

print(move)

Solution (Python):
def run():
  N = int(input())
  for i in range(int(N**(1/2)), 0, -1):
    if N % i == 0:
      j = N//i
      print(i+j-2)
      break
  
if __name__ == '__main__':
  run()

Solution (Python):

import sys
import math

def solve(N: int):
    INF = 10000000000000
    result = INF
    for a in range(1, int( math.sqrt(N) ) + 1):
        if N % a:
            continue
        result = min(result, a + int( N / a ) - 2)
    print(result)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
r=10**12
for i in range(1,int(n**0.5)+1):
	if n%i==0:
		r=min(r,i+n//i-2)
print(r)

Solution (Python):
import math

N = int(input())

sqN = math.floor(math.sqrt(N))

s = N - 1
for i in range(1,sqN+1):
    if N % i == 0:
        s = min(s,(N // i) + i - 2)

print(s)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    ans = N - 1
    max_sq = int(math.sqrt(N)) + 1
    for i in range(1, max_sq):
        if N % i != 0:
            continue
        j = N // i
        ans = min(ans, i+j-2)
    print(ans)
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
def main():
    N = int(input())
    ans = N-1
    max_sq = int(math.sqrt(N)+1)
    for i in range(1, max_sq):
        if N % i != 0:
            continue
        j = N // i
        ans = min(ans, i+j-2)
    print(ans)
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
def main():
    N = int(input())
    ans = 10e+12 
    max_sq = int(math.sqrt(N)+1)
    for i in range(1, max_sq):
        if N % i == 0:
            j = N // i
            ans = min(ans, i+j-2)
    print(ans)
main()

Solution (Python):
from __future__ import print_function

import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return















































def main():
    n = int(input().strip())

    a=1;b=n
    for i in range(1, int(n**.5)+1):
        if n%i==0:
            a=i
            b=n//a
    
    
    print(a+b-2)
        
    return

if __name__ == '__main__':
    main()



    '''
    for given N (2<=N<=10**12)
    find a_cnt such that           //  := (i_ans - i) + (j_ans - j)

    
    let T such that { (i,j) | (i,j \in [1...10**12] ) and (i*j == N) }
    

    let (i_ans, j_ans) \in T such that

    ( (i_ans, j_ans) \in T )
    and
    ( for all (i,j) \in T . (i_ans - 1) + (j_ans - 1) <= (i-1)+(j-1) )

    find a_cnt := (i_ans - 1) + (j_ans - 1)


    //
    [1, 1] -> [i_ans, j_ans] such that ( i_ans * j_ans = N )
    
    
    (1 <= i_ans <= , 1 <= j_ans <= )
    
    
    
    
    
    
    '''

Solution (Python):
import math
x = int(input())
s = int(math.sqrt(x))
c = 0
while True:
    if x % s == 0:
        c = x // s
        break
    s = s - 1
print(s + c - 2)

Solution (Python):
from math import sqrt


N = int(input())
res = N
for x in range(1, int(sqrt(N)) + 1):
    if N % x == 0:
        y = N // x
        res = min(res, x + y - 2)

print(res)

Solution (Python):
N = int(input())

max_i = int(N ** 0.5)

for i in range(max_i, 0, -1):
    if N % i == 0:
        x = i
        y = N // i
        break

ans = x + y - 2

print(ans)

Solution (Python):
N = int(input())

x = int(N ** 0.5)

for i in range(x, 0, -1):
    if N % i == 0:
        y = N // i
        break

ans = N // y + y - 2

print(ans)

Solution (Python):
N = int(input())

x = int(N ** 0.5)

for i in range(x):
    if N % x == 0:
        y = N // x
        break
    x -= 1

ans = x + y - 2

print(ans)

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit = 10 ** 6
N = int(input())


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


def dfs(i, x):
    global N
    global res
    if i != len(prime_keys):
        p = prime_keys[i]
        for num in range(prime[p] + 1):
            x_ = x * (p ** num)
            y = N // x_
            res = min(res, x_ + y - 2)
            dfs(i + 1, x_)


prime = Counter(prime_factorize(N))
prime_keys = list(prime.keys())
res = N * 2
dfs(0, 1)
print(res)

Solution (Python):
n=int(input())
max_a = int(n**0.5)
for a in range(max_a,0,-1):
    if n%a == 0:
        print(a+n//a-2)
        break

Solution (Python):
n = int(input())
factor = []

for i in range(1,int(n**0.5)+1):
    if n%i == 0:
        factor.append(i + n//i)

factor.sort()
print(factor[0]-2)

Solution (Python):
N=int(input())
ans=[]
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i+n//i)
    divisors.sort()
    return divisors
ans=make_divisors(N)
print(ans[0]-2)

Solution (Python):

def make_divisors(n):
    divisors = []
    ans = 10**12
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            x, y = i, n//i
            if ans > x-1 + y-1:
                ans = x - 1 + y - 1
    return ans

N = int(input())
print(make_divisors(N))

Solution (Python):
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)

    
    return divisors
N = int(input())

list = make_divisors(N)
M = len(list)
if M % 2 == 1:
    ans = (list[-1]-1)* 2
else:
    ans = (list[-1]-1) + (list[-2]-1)
print(ans)

Solution (Python):
import math
N = int(input())
list = []
for i in range(int(math.sqrt(N))):
    if(N%(i+1) == 0):
        list.append([i+1, N//(i+1)])
min = 1000000000000
for i in list:
    if(min > i[0]+i[1]):
        min = i[0]+i[1]
print(min-2)

Solution (Python):
import math
n = int(input())

a = []
m = math.floor(n**(1/2))
for i in range(1,m + 1):
    if n % i == 0:
        a.append(i + (n/i) - 2)

print(int(min(a)))

Solution (Python):
import math
n = int(input())
n2 = int(math.sqrt(n))

mini = n - 1

for i in range(1,n2+1):
  if n%i == 0:
    mini = min(mini,n//i + i - 2)

print(mini)

Solution (Python):
import math
A = int(input())
B = int(math.sqrt(A))
count = 0
for i in reversed(range(1,B+1)):
	if count == 0:
		if A % i == 0:
			print(int(i+A/i-2))
			count += 1

Solution (Python):
n = int(input())

ans = n
for i in range(1,10**6+1):
    if n%i == 0:
        j = n//i
        ans = min(ans, i-1+j-1)
print(ans)

Solution (Python):
num = int(input())
print(min(i + num // i - 2 for i in range(1, int(num ** (1 / 2)) + 1) if not num % i))

Solution (Python):
import sys
import itertools
import bisect

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

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


n = ni()

li = make_divisors(n)

res = 10 ** 12 + 1
m = len(li)
for i in range(m//2 + 1):
    res = min(li[i] - 1 + li[-(i + 1)] - 1, res)
    
print(res)

Solution (Python):
from math import sqrt
n = int(input())
for i in range(int(sqrt(n)), 0, -1):
  if n%i == 0:
    print(i + n//i - 2)
    break

Solution (Python):
import math
N=int(input())
c=[]
flag=0
for x in range(1, math.ceil(pow(N,1/2))+1):
    if N%x==0:
        c.append(x+N//x-2)
print(min(c))

Solution (Python):
n = int(input())
for i in range(-int(-n**0.5//1),0,-1):
  if n%i==0:
    a=i
    b=n//i
    break
print(a+b-2)

Solution (Python):
import math

def isPrime(n):
  if n == 2:
    return True
  if n % 2 == 0:
    return False
  m = math.floor(math.sqrt(n)) + 1
  for p in range(3, m, 2):
      if n % p == 0:
        return False
  return True

N=int(input())

result=1000000000000

i=1

if isPrime(N)==True:
    print(N-1)
else:
    for i in range(1,int(math.sqrt(N))+1):
        i+=1
        if(N%i==0):
            buf=i-1+N/i-1
            if(buf <= result):
                result=buf
    print(int(result))

Solution (Python):

N = int(input())
root_N = N ** 0.5
ans = 10 ** 13

for i in range(1, int(root_N)+1):
    if N % i == 0:
        j = N / i
        ans = min(ans, i+j-2)

print(int(ans))

Solution (Python):
import math

N = int(input())

res = 10 ** 15
for i in range(1, int(math.sqrt(N))+1):
    if N%i == 0:
        res = min(res, N // i + i)

print(res-2)

Solution (Python):
import sys
from math import sqrt

[nstr] = sys.stdin.readline().split(" ")
n = int(nstr)

for i in range(int(sqrt(n)), 0, -1):
    r = float(n) / float(i)
    if not r.is_integer():
        continue
    j = int(r)
    print(i-1 + j-1)
    exit()

Solution (Python):
N = int(input())
n = int((N**(1/2))//1)
s =[0]*(n+10)
for i in range(1,n+1):
  if N%i == 0:
    s[i] = i
maxs = s[0]
for j in range(1,n+1):
  if s[j] > maxs:
    maxs = s[j]
p = int(N/maxs)
walk = p + maxs - 2
print(walk)

Solution (Python):
from math import sqrt

N = int(input())
m = 10**13
for i in range(1, int(sqrt(N))+1):
    if N % i == 0:
        m = min(m, i + N // i - 2)
print(m)

Solution (Python):
N = int(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


List = make_divisors(N)

if len(List) % 2 == 1:
    print(List[len(List) // 2] * 2 - 2)
else:
    print(List[len(List) // 2 - 1] + (List[len(List) // 2] - 2))

Solution (Python):


import math
n = int(input())
r_n = int(math.sqrt(n))+1
m = 10 ** 12
for i in range(1,r_n):
  if n % i == 0 and m > i + n / i - 2:
    m = i + n / i - 2
 
print(int(m))

Solution (Python):
import numpy as np
n = int(input())

ans = np.inf
for i in range(1, 10**6+1):
    if n%i: continue
    j = n//i
    ans = min(ans, i +j -2)

print(ans)

Solution (Python):
from math import sqrt
n=int(input())

ans=0
for i in range(1,int(sqrt(n))+1):
  if n%i==0: ans = i+n//i-2

print(ans)

Solution (Python):
こるぼー = print

n = int(input())

ans = float("inf")
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        j = n // i
        ans = min(ans, i + j - 2)

こるぼー(ans)

Solution (Python):
n = int(input())

こるぼー = float("inf")
for i in range(1, int(n ** 0.5) + 1):
    if n % i == 0:
        j = n // i
        こるぼー = min(こるぼー, i + j - 2)

print(こるぼー)

Solution (Python):
n = int(input())
step = float('inf')
import math
k = math.floor(math.sqrt(n))+1
for i in range(1,k+1):
    div, mod = divmod(n,i)
    if mod == 0 and i + div - 2 < step:
        step = i + div -2
print(step)

Solution (Python):
import math
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a_max = int(math.sqrt(n))
    min_v = 10 ** 12
    for a in range(1, a_max+1):
        if n % a == 0:
            min_v = min(min_v, a + (n//a) - 2)
    print(min_v)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math

def solve(N: int):
    ans = 100000000000000000
    amax = math.floor(math.sqrt(N))
    for i in range(1,amax+1):
        if N % i == 0:
            ans = min(ans, i+(N/i)-2)
    print(math.floor(ans))
    
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
import math
m=int(math.sqrt(n))
while n%m!=0:
  m-=1
else:
  print(int(m+(n/m)-2))

Solution (Python):
n = int(input())

res = float('inf')
for x in range(1, int(n**0.5) + 1):
    if n % x == 0:
        res = min(res, x-1 + n//x -1)
print(res)

Solution (Python):
import math
N=int(input())

a=[]
b=[]
z=[]
for i in range(1,round(math.sqrt(N))+1):
    if N%i==0:
        a.append(i)
        b.append(N//i)
    else:
        continue

for j in range(len(a)):
    z.append(a[j]+b[j]-2)

print(min(z))

Solution (Python):
N = int(input())
ans = int(10e13)
i = 1

while i * i <= N:
  if N % i == 0:
    ans = min(ans, N // i - 1 + i - 1)
  i += 1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
s = 0
N = int(input())
for i in range(1, int(N**(1/2))+1):
    if N%i == 0 and i > s:
        s = i
print(s+(N//s)-2)

Solution (Python):
import math
n=int(input())
m=math.floor(n**0.5)
while(n%m!=0):
  m -= 1
print(m + n//m - 2)

Solution (Python):
N = int(input())

s = int(N**0.5) + 1   
j = 1
for i in range(1, s):
    if N % i == 0:
        j = i
    
ans = (N // j - 1) + (j - 1)
    
print(ans)

Solution (Python):
N = int(input())
max_int = int(N**0.5)
for v in reversed(range(max_int+1)):
    if N % v == 0:
        w = int(N/v)
        print(v+w-2)
        break
    else:
        pass

Solution (Python):
import math
N = int(input())
ij = int(math.sqrt(N)//1)

for i in range(1,ij+1):
    if N%i == 0:
        num = i

print((num-1)+(N//num-1))

Solution (Python):
from math import sqrt, ceil

N = int(input())
sq = ceil(sqrt(N))
min_st = N - 1

for i in range(2, sq + 1):
    if N % i == 0:
        if i + N // i - 2 < min_st:
            min_st = i + N // i - 2

print(min_st)

Solution (Python):
from math import sqrt

n = int(input())

ans = float('inf')
for i in range(1, int(sqrt(n)+1)):
    if n % i == 0:
        ans = min(ans, i + n//i - 2)
print(ans)

Solution (Python):
import math

N = int(input())



for i in range(int(math.sqrt(N)), 0, -1):
    
    if N % i == 0:
        ans = i+(N//i)-2
        print(ans)
        break

Solution (Python):
import math

def main():
    N = int(input())
    rt_N = math.ceil(math.sqrt(N))

    mn_ij = 10**12
    for i in range(1, rt_N+1):
        if N % i == 0:
            j = N // i

            mn_ij = min(mn_ij, i+j)
        
        mn_ij = min(mn_ij, i+j)

    
    print(int(mn_ij-2))

if __name__ == "__main__":
    main()

Solution (Python):
n= int(input())
tmp = float('inf')
for i in range(1,1000001):
	if n % i == 0:
		tmp =min(tmp,i + n/i -2)

print(int(tmp))