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):
N = int(input())
ans = float('inf')
for i in range(1,int(N**0.5) + 1):
    tmp = N / i
    if tmp.is_integer():
        if tmp == N:
            tmp_ans = N - 1
        else:
            tmp_ans = int(tmp) + i - 2
        if ans > tmp_ans:
            ans = tmp_ans
print(ans)

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

Solution (Python):
import math

def finding(n):
    nn = math.floor(math.sqrt(n))
    if nn*nn == n:
        return nn, nn
    else:
        for i in range(nn, 0, -1):
            if n % i == 0:
                return i, n//i


n = int(input())
nn1, nn2 = finding(n)
print(nn1+nn2-2)

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

Solution (Python):
n = int(input())
a = int(n**0.5)
i = 1
while i <= a:
    if n % i == 0:
        b = i
    i = i + 1
c = int(n / b)
print(b+c-2)

Solution (Python):
import math

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

Solution (Python):

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

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

import math
rtn = math.ceil(math.sqrt(n))
for i in range(rtn):
  i = rtn - i
  if n%i == 0:
    print(i + int(n/i) - 2)
    break

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

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

Solution (Python):
import math

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

for i in range(m):
  if n % (m - i) == 0:
    print(int(m-i + n/(m-i) - 2))
    break

Solution (Python):
def solve(n):
    ans = n-1
    i = 1
    while i*i <= n:
        if n % i == 0:
            ans = min(ans, i+n//i-2)
        i += 1
    return ans

n = int(input())
print(solve(n))

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

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

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

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

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

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

Solution (Python):

import math

def main():
    n = int(input())
    res = float('inf')

    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            res = min(res, (i - 1) + (n // i - 1))

    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
import math

N = int(input())
lst = [n for n in range(1, int(math.sqrt(N) + 1))]
for x in lst[::-1]:
    if N % x == 0:
        print((x - 1) + (N // x - 1))
        exit()

Solution (Python):
import math
N=int(input())    
NS=math.floor(N**0.5)

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

Solution (Python):



def main():
    n = int(input())
    ans = float('inf')

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

end = int(n**0.5) + 2
dif = []
for i in range(1, end):
  if n % i == 0:
    dif.append(abs(n//i + i) - 2)
print(min(dif))

Solution (Python):
import math
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())
min=N-1
parr=make_divisors(N)
for i in range(len(parr)):
    if(parr[i]+N//parr[i]-2<N):
        min=parr[i]+N//parr[i]-2
print(min)

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

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

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

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

Solution (Python):
import math

N = int(input())
 
mi = 10**100

for a in range(1, int(math.sqrt(N))+1, 1):
  if N % a == 0:  
    b = N//a
    if mi > a+b-2:
      mi = a+b -2
print(mi)

Solution (Python):
n=int(input())
l=list(range(2,int(n**0.5//1+1)))
ans=n-1
count=0
for i in l:
    if n%i==0:
        a=i+int(n/i)-2
        ans=min(ans,a)
        count+=1
print(ans if count>0 else n-1)

Solution (Python):
N = int(input())
ans = float('inf')

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

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

Solution (Python):
import math
sqrt = math.sqrt
n = int(input())
m = int(sqrt(n)) +1

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

print(ans)

Solution (Python):
n = int(raw_input())
ans_xy =[]
tmp = []
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    ans_xy.append(i)
    ans_xy.append(n/i)
    tmp.append(ans_xy)
    ans_xy = []
ans=tmp[0][0]+tmp[0][1]-2
for i in range(1,len(tmp)):
  ans = min(ans,tmp[i][0]+tmp[i][1]-2)
print ans

Solution (Python):

import sys
import math
from copy import deepcopy
from collections import deque, defaultdict
from operator import mul
from functools import reduce
sys.setrecursionlimit(20000000)




def main():
    N = int(input())
    min_s = 10 ** 12

    for i in range(1, int(math.sqrt(N))+1):
        if N % i == 0:
            if min_s > i+(N/i):
                min_s = i + (N/i)

    print(int(min_s - 2))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [i for i in range(1,int((n)**0.5 +1))]
a.reverse()

for j in range(len(a)):
    if n%int(a[j]) == 0:
        print(int(int(a[j]) + n//int(a[j]) -2))
        break

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

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

Solution (Python):
import math
N=int(input())
sq = int(math.sqrt(N))
if sq**2 == N:
    print(2*sq-2)
else:
    for i in range(sq):
        j=sq-i
        if not N%j:
            print(j+N//j-2)
            break

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

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

Solution (Python):
import math


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

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
n = 1
ans = N+1
while n**2 <= N:
    if N % n != 0:
        n+=1
        continue
    ans = min(ans, N // n + n)
    n+=1
print(ans-2)

Solution (Python):
import math

INF = 1e12
n = int(input())
cnt = INF
m = math.sqrt(n)

for i in range(1, math.ceil(m) + 1):
  if n % i == 0:
    j = n // i
    cnt = min(cnt, i + j - 2)
  else:
    continue

print(cnt)

Solution (Python):
import math

INF = 1e12
n = int(input())
ans = INF
m = math.sqrt(n)
for i in range(1,math.ceil(m)+1):
    if n%i==0:
        j = n//i
        ans = min(ans, i+j-2)
    else:
        continue
print(ans)

Solution (Python):
import math
n = int(input())
min = n - 1

split = n / math.sqrt(n)


for i in range(int(split),0,-1):
    if n % i == 0:
        min = i + n/i -2
        break

print(int(min))

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

Solution (Python):


n = int(input())

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

print(int(sum(mina)-2))

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

Solution (Python):
from collections import defaultdict


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


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


def answer(f, d):
    a = 1
    for i in f:
        a *= i
        if a > d:
            return inf
    return a + N // a - 2


def sa(f):
    d = inf
    for fi in f:
        d = min(d, answer(fi, d))
    return d


def comb(f):
    c = [[]]
    for k in f:
        ct = [[k] * i for i in range(1, f[k] + 1)]
        ci = c.copy()
        for cti in ct:
            for cii in ci:
                c.append(cii + cti)
    return [[1]] + c[1:]



N = int(input())
inf = 999999999999
factors = factorize(N)
print(sa(comb(factors)))

Solution (Python):
import math
n=int(input())
x=1
for i in range(2, math.ceil(math.sqrt(n))+1):
  if n%i==0 and x<i:
    x=i
print(x+n//x-2)

Solution (Python):
N = int(input())
ans = N - 1

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

print(ans)

Solution (Python):
import math
N = int(input())
L = []

def f(x):
  n = int(math.sqrt(x))
  for i in range(n,0,-1):
    if x % i == 0:
       return i
result = f(N) + N // f(N) - 2
print(result)

Solution (Python):
import math
n = int(input());
s = n - 1
for i in range(1,int(math.sqrt(n))+1):
    t = n / i
    if t.is_integer() == True and s > (t-1)+(i-1):
        s = (t-1)+(i-1)
print(int(s))

Solution (Python):
import math

n = int(input())


def make_divisors(n):
    min_sum = 10**12
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            a = i
            
            
            b = n//i
        if a+b < min_sum:
            min_sum = a+b
    
    
    return min_sum

print(make_divisors(n)-2)

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

sqrtN = int(math.sqrt(N)) + 1

for a in range(1, sqrtN)[::-1] :
    if N % a == 0:
        b = N / a
        if AB_MIN > a + b - 2:
            AB_MIN = a + b - 2

if AB_MIN == 1000000000000:
    AB_MIN = N - 1

print(int(AB_MIN))

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

Solution (Python):




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


n=int(input())
div=divisors(n)
res=float('inf')
for e in div:
    d=e+n//e-2
    res=min(res,d)
print(res)

Solution (Python):
N = int(input())
import math
n = math.floor(N**0.5)
s = N + 1
for i in range(1,n + 1):
    if N % i == 0:
        a = i + (N/i)
        s = a
print(int(s-2))

Solution (Python):
def dis(xa,ya):
    return abs(xa - 1) + abs(ya - 1)

n = int(input())
ans = 2*n
i = 1
while i*i <= n:
    if (n % i == 0):
        ans = min(ans, dis(i,n//i))
    i += 1
print(ans)

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

for i in range(1, int(N ** (1/2)+1)):
    if (N % i == 0):
        a = i

b = N // a

ans = (a - 1) + (b - 1)

print(ans)

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

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

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)

    divisors.sort()
    return divisors




i= 0

A = int(input())
A_d = make_divisors(A)
count = 0

while A_d[i]<= A**0.5:
    if A == 1:
        count = 0
        break
    count = i
    i += 1

result = []
result.append(A_d[count])
result.append(int(A / A_d[count]))



result_sum = result[0] + result[1] - 2

print(result_sum)

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

Solution (Python):
import math

N = int(input())
a = math.sqrt(N)
b = math.floor(a)
for i in range(b):
    c = N/(b-i)
    if c.is_integer():
        ans = c-2+b-i
        break;
print(int(ans))

Solution (Python):
import math

N = int(raw_input())

limit = int(math.sqrt(N)) + 1
n = float(N)
minimum = (1, 1, N)
for i in range(1, limit):
    j = n / i
    if not j.is_integer():
        continue
    j = int(j)
    distance = i + j - 2
    if minimum[2] > distance:
        minimum = i, j, distance
print(minimum[2])

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

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

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

l = []
for i in range(1,int(num+1)):
    if n % i == 0:
        l.append(i)
ans = int(n/max(l)) -1 + int(max(l)) - 1
print(ans)

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


N = int(input())


import math
M = int(math.sqrt(N))

ans = N
for i in reversed(range(1, M+1)):
    if float.is_integer(N/i):
        a = i
        b = int(N/i)
        c = a + b -2
        ans = min(ans, c)
        
print(ans)

Solution (Python):
N = int ( input() )
check_list = []
for i in range( 1, int( N**( 1/2 )+1 ) ):
    check = 0
    if N % i == 0:
        check = i + int( N / i ) - 2
        check_list.append(check)

print( min(check_list) )

Solution (Python):
import math

N = int(input())
ans = N-1
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())
pair = (1,n)

from math import sqrt
mid = int(sqrt(n))
for i in range(mid,0,-1):
    if n % i==0:
        pair = (i,n//i)
        break
print(sum(pair)-2)

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

ans = n+1
for x in range(int(n**0.5), 1, -1):
    if(n % x == 0):
        ans = x + n//x
        break
print(ans-2)

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

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

Solution (Python):
n = int(input())
i = int(n**(1/2))
while n%i:
	i-=1
j = n//i
ans = (i - 1) + (j - 1)

print(ans)

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

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

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

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

print(ans)

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

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

divisors_list = make_divisors(N)

if len(divisors_list)%2==0:
    print(divisors_list[int(len(divisors_list)/2)] + divisors_list[int(len(divisors_list)/2 -1)] -2)
else:
    print(divisors_list[int((len(divisors_list)-1)/2)] + divisors_list[int((len(divisors_list)-1)/2)] -2)

Solution (Python):
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def divisors(N: int) -> set:
    """ 約数の列挙・個数 """
    from math import sqrt

    
    s = {1, N}
    
    for i in range(2, int(sqrt(N))+1):
        
        if N % i == 0:
            s.add(i)
            s.add(N // i)
    return s

N = INT()

ans = 10 ** 18
for div in divisors(N):
    ans = min(ans, div+N//div-2)
print(ans)

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



ans = N
i = 1
while i * i <= N:
    
    
    div, mod = divmod(N, i)
    
    if mod == 0:
        
        
        
        
        ans = min(ans, ((i - 1) + (div - 1)))
    
    i += 1
    
print(ans)

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

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

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

import math
l=[]
v=n+1-2

for a in range(1,int(math.sqrt(n))+1):
  if n%a==0:
    b=int(n/a)
    temp=a+b-2
    if v>temp:
      v=temp
    
print(v)

Solution (Python):

a = int(input())

arr = []

for i in range(1,int(a**0.5)+1):
    b = a / i
    
    if a == int(b)*i:
        arr.append([int(i),int(b)])

arrb = [sum(arr[i]) for i in range(len(arr))]
print(min(arrb)-2)

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

import math
l=[]
for a in range(1,int(math.sqrt(n))+1):
  if n%a==0:
    b=int(n/a)
    l.append(a+b-2)
    
print(min(l))

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
N = int(read())
U = 10**6 + 100
x = np.arange(1,U,dtype=np.int64)
div = x[N%x==0]
ans = (div + (N//div)).min() - 2
print(ans)

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

for i in range(1, int(n**0.5)+1):
    if n%i == 0:
        x = i
        y = n/i

print(int(x+y-2))

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

ans = 10**18

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

print(ans)

Solution (Python):
import math

N = int(input())
n = math.sqrt(N)
n = math.ceil(n) 


min_diff = N
a = -1
b = -1

for i in range(1,n+1):
    if N % i == 0:
        j = N // i
        if min_diff > abs(i-j):
            a = i
            b = j
            min_diff = abs(i-j)

print(a+b-2)

Solution (Python):
from enum import Enum
from queue import Queue

import numpy as np

import collections
import bisect
import sys
import math

from scipy.sparse.csgraph import floyd_warshall

BIG_NUM = 200000000000
MOD = 1000000007
EPS = 0.000000001

N = int(input())
ans = N

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

print(ans)

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

aa = int(Input**0.5)

for i in range(1,aa+1):
  if Input%(aa+1-i) == 0:
    num1 = aa+1-i
    num2 = Input/(aa+1-i)
    break
print(int(num1 + num2 -2))

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

import math
sq = math.sqrt(n)
a = math.floor(sq)

for i in reversed(range(1,a+1)):
    if n%i==0:
        a = i
        b = n/i
        print(int(a+b-2))
        exit()

print(n-1)

Solution (Python):
import math

def f(x):
    y = int(math.sqrt(x)) + 1
    for i in reversed(range(1,y)):
        if x % i == 0:
            a = x/i -1
            b = i -1
            sum = a+b
            return print(str(int(sum)))

f(int(input()))

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

a = 1
ans = 2**64
while a*a<=N:
    b = N/a
    if b!=int(b):
        a += 1
        continue
    b = int(b)
    ans = min(ans, a+b-2)

    a += 1

print(ans)

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

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

L = make_divisors(n)
P = []
if math.sqrt(n) % 1 != 0:
    P.append(L[-1])
    P.append(L[-2])
else:
    P.append(L[-1])
    P.append(L[-1])

print(sum(P)-2)

Solution (Python):
import math
import copy

n=int(input())

i=int(n**0.5)
j=0


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

Solution (Python):
N = int(input())
lis = []
for a in range(1,int(N**0.5)+1):
    b = N // a
    if a * b == N:
       lis.append(a + b - 2)
print(min(lis))

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

ans = N

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

Solution (Python):

import math
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n = ni()
sqrtn = int(math.sqrt(n))
for i in range(sqrtn, 0, -1):
    if n%i == 0:
        j = n // i
        print(i+j-2)
        break

Solution (Python):
import math

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

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())
a = make_divisors(n)
ans = 11451481019191919191919
for i in a:
	ans = min(ans, i+n/i)
	
print(int(ans-2))

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

l=list()

m=n+1
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    m = min(m, i+n//i)

print(m-2)

Solution (Python):
import math

n = int(input())
r_n = int(math.sqrt(n))
s = []

for i in range(1, r_n+1):
    if n%i == 0:
        s.append(n//i + i - 2)
print(min(s))

Solution (Python):
from math import sqrt

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

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


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
qa=[n+1]
for i in range(1,int(n**(1/2))+1):
	if n%i==0:
		qa.append(i-1+(n//i)-1)
print(min(qa))

Solution (Python):
m = int(input())
a = {}
for i in range(1, int(m**0.5)+1):
    if m % i == 0:
        n = m // i
        a[i] = n
ans =[]
for i,j in a.items():
    b = i + j
    ans.append(b)

print(min(ans)-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())
a = make_divisors(N)
b = []
c = []

for j in range(len(a)):
    for k in range(len(a)):
        if a[j] * a[k] == N:
            b.append([a[j],a[k]])

for l in range(len(b)):
    c.append(b[l][0] + b[l][1])

print(min(c) - 2)

Solution (Python):


import math

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

    for k in range(N2, 0, -1):
        if N%k == 0:
            print(N//k + k - 2)
            return


if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
ans = 0

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

print(ans)

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

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

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

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

def rangesqrt(n):
    i = 1
    while i * i <= n:
        yield i
        i += 1

pos = ((d, N // d) for d in rangesqrt(N) if N % d == 0)
print(min(x - 1 + y - 1 for x, y in pos))

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

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

Solution (Python):
from math import sqrt


def Calc_Divisors(num):
    divisors = []
    for i in range(1, int(sqrt(num)) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num//i)
    divisors.sort()
    return divisors


if __name__ == "__main__":
    N = int(input())

    div = Calc_Divisors(N)

    ans = N
    for d in div:
        a = d
        b = N // a
        c = a + b - 2
        if c < ans:
            ans = c

    print(ans)

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

Solution (Python):

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

print(s)

Solution (Python):
import math


N = int(input())
a = math.sqrt(N)

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

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 math
n=int(input())
nsq=math.sqrt(n)
for i in range(int(nsq),0,-1):
	if n%i==0:
		print(i+(n//i)-2)
		break

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

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

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

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

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

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

a = n
b = 1

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

print(a+b-2)

Solution (Python):
import numpy as np

n = int(input())

ar = np.arange(1, 10 ** 6 + 10)
div = ar[n % ar == 0]

ans = (div + n // div - 2).min()

print(ans)

Solution (Python):
import sys
import math
import bisect
 
 
sys.setrecursionlimit(1000000000)
def input():
    return sys.stdin.readline().strip()
 
def iinput():
    return int(input())
 
def finput():
    return float(input())
 
def tinput():
    return input().split()
 
def rinput():
    return map(int, tinput())
 
def rlinput():
    return list(rinput())
 
def modst(a, s):
    res = 1
    while s:
        if s % 2:
            res *= a
        a *= a
        s //= 2
    return res

 
def main():
    n = iinput()
    for i in range(int(n ** 0.5) + 1, 0, -1):
        if n % i == 0:
            print(i - 2 + n // i)
            break
main()

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

Solution (Python):
import math


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 = int(input())
N_diviors = make_divisors(N)
N_diviors_len = len(N_diviors)
N_sqrt = math.sqrt(N)

for i in range(N_diviors_len):
    if N_sqrt <= N_diviors[i]:
        print(N_diviors[i] + int(N / N_diviors[i]) - 2)
        break

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


def devide2(x):
    ar = []
    ar.append([1, x])
    for i in range(2, int(-(-x ** 0.5 // 1)) + 1):
        if x % i == 0:
            ar.append([i, x // i])
    return ar


kumi = devide2(n)


ans = 10 ** 12 + 1000
for i in range(len(kumi)):
    ans = min(ans, sum(kumi[i]))

print(ans - 2)

Solution (Python):
from operator import itemgetter
n = int(input())

list_p = []
for i in range(1, int(n**0.5)+1):

    j = n / i

    if j.is_integer():
        list_p.append([i, j, i+j])

list_sorted = sorted(list_p, key=itemgetter(2))
print(int(list_sorted[0][0] + list_sorted[0][1] - 2))

Solution (Python):
import numpy as np

def isPrime(n):
  s = int(np.sqrt(n))
  for i in range(2, s+1):
    if n % i == 0:
      return False
  return True

def sum_devisor(n):
  s = int(np.sqrt(n))
  while(n % s != 0):
    s -= 1
  return s + n/s

n = int(input())
if isPrime(n) == True:
  print(n-1)
else:
  print(int(sum_devisor(n)-2))