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())

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

d = make_divisors(N)


Ans_min = 1+N-2

for x in d:
  y = N/x
  Ans = x+y-2
  if Ans < Ans_min:
    Ans_min = Ans
    
print(int(Ans_min))

Solution (Python):
def make_div(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()
    
    if (len(divisors) + 1) % 2 == 1:
        x = (len(divisors) + 1) // 2 - 1
        ans1 = divisors[x] - 1 + divisors[x + 1] - 1
        print(ans1)
    
    else:
        y = len(divisors) // 2
        ans2 = 2 * (divisors[y] - 1)
        print(ans2)
    return divisors


m = int(input())
make_div(m)

Solution (Python):
n = int(input())
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()
S=len(divisors)

if S%2 == 0:
  print(divisors[int((S/2)-1)] + divisors[int(S/2)] - 2)
else:
  print((divisors[int((S-1)/2)]-1)*2)

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())
Nlist = make_divisors(N)
L = len(Nlist)
if L%2==0:
    print(Nlist[int(L/2)]+Nlist[int(L/2)-1]-2)
else:
    print(int(math.sqrt(N)*2-2))

Solution (Python):
N=int(input())
A=[]
B=[]
i=1
while True:
    if N/i>=i:
        if N%i==0:
            A.append(N/i)
            B.append(i)
        i+=1
    else:
        break
print(int(A[-1]+B[-1]-2))

Solution (Python):
from math import sqrt

N = int(input())

it = int(sqrt(N))

for i in range(it, 0, -1):
    remain = N % i
    if remain == 0:
        shou = N / i

        print(int(i+shou-2))
        break

Solution (Python):
import statistics
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())
nums = make_divisors(n)
a = statistics.median_low(nums)
b = n / a
print(int(a+b-2))

Solution (Python):
import numpy as np

N=int(input())
a=int(np.sqrt(N))
b=float('inf')
for i in range(1,a+1):
    if N%i==0:
        if i+N//i<b:
            ans=i

print(ans+N//ans-2)

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

import math

def is_prime(n):
  for k in range(int(math.sqrt(n)) + 1,0,-1):
    if n % k == 0:
      print(int(k-1+n/k-1))
      break


is_prime(n)

Solution (Python):
import math
  
a = int(input())
rs = []

for i in list(range(1,math.floor(a**0.5) + 1)):
  q, mod = divmod(a, i)
  if mod == 0:
    rs.append([i, q])
  else:
    pass
  
print(sum(rs[len(rs) - 1]) - 2)

Solution (Python):
import heapq

N = int(input())

def divisorize(fct):
    b, e = fct.pop()  
    pre_div = divisorize(fct) if fct else [[]]
    suf_div = [[(b, k)] for k in range(e + 1)]
    return [pre + suf for pre in pre_div for suf in suf_div]

def factorize(n):
    fct = []  
    b, e = 2, 0  
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def num(fct):
    a = 1
    for base, exponent in fct:
        a = a * base**exponent
    return a

fct = factorize(N)
steps = []
for div in divisorize(fct):
    a = num(div)
    b = N//a
    step = a+b-2
    steps.append(step)
    
heapq.heapify(steps)
print(heapq.heappop(steps))

Solution (Python):
import math
def main2():
    def get(N):
        res = set()
        for i in range(1, int(math.sqrt(N)+2)):
            if N % i == 0:
                res.add(i)
                res.add(N // i)
        return res
    N = int(input())
    mini = 10 ** 15
    for c in get(N):
        d = N // c
        e = c + d - 2
        mini = min(mini, e)
    print(mini)
main2()

Solution (Python):
def factorization(n):
    arr = []
    temp = n
    maxVal = 10 ** 13
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            value = (temp//i) + i - 2
            if value < maxVal :
                maxVal = value
            
            
            
            
            
            
            

    
    

    if maxVal == 10 ** 13:
        maxVal = n - 1

    return maxVal

n = int(input())
print(factorization(n))
'''
factList = factorization(n)

max = 10 ** 13
if len(factList) == 1 :
    print(factList[0] - 1)
    exit()


pair = [1, 1]






print(factList)
print(pair)
print(sum(pair) - 2)
'''

Solution (Python):
N = int(input())
'''
def factorization(n):
    arr = [1,1]
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            while temp%i==0:
                if arr[1] >= arr[0]:
                    arr[0] = arr[0]*i
                else:
                    arr[1] = arr[1]*i
                temp //= i   
    if temp!=1:
        if arr[1] >= arr[0]:
            arr[0] = arr[0]*temp
        else:
            arr[1] = arr[1]*temp
    if arr==[1,1]:
        arr[0] =n
    return arr

facro = factorization(N)
print(facro[0] +facro[1] -2)
'''
min_num = 1
 
for i in reversed(range(2,int(-(-N**0.5//1))+1)):
    if N%i == 0:
        
        min_num = i
        break

print(min_num + N//min_num-2)

Solution (Python):
import numpy as np

n = int(input())
for i in range(int(np.floor(np.sqrt(n))), 0, -1):
    if n % i == 0:
        print(i + int(n / i) - 2)
        break

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

    
    
    return divisors

def resolve():

    a = int(input())
    divisors = make_divisors(a)
    anss= [] 
    for i in range(len(divisors)):
        anss.append(sum(divisors[i]))
    print(min(anss) - 2)

    
resolve()

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

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

L = factorization(N)
distance = [l[0] + l[1] for l in L]

m = min(distance)
print(m-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

num = int(input())
yakusu_list = make_divisors(num)
nine = [1,2,3,4,5,6,7,8,9]
yakusu_pair_list = []
a = 0
b = 0
for i, yakusu in enumerate(yakusu_list):
    
    if len(yakusu_list) % 2 == 0:
        if i % 2 == 0:
            a = yakusu
        else:
            b = yakusu
            yakusu_pair_list.append([a, b])
    else:

        if i == len(yakusu_list) -1:
            yakusu_pair_list.append([yakusu, yakusu])
    
        else:
            if i % 2 == 0:
                a = yakusu
            else:
                b = yakusu
                yakusu_pair_list.append([a, b])
best_pair = yakusu_pair_list[0]
best_move_num = best_pair[0] + best_pair[1] -2
for yakusu_pair in yakusu_pair_list:
    this_move_num = yakusu_pair[0] + yakusu_pair[1] -2
    if this_move_num < best_move_num:
        best_pair = yakusu_pair    
        best_move_num = this_move_num
print(best_move_num)

Solution (Python):













def make_divisors2(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())

t = make_divisors2(n)
li = []
for i in t:
    li.append(i + n / i)

print(int(min(li)-2))

Solution (Python):
def cal(n):
  min_num = -1
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      num = i + (n//i)
      if min_num < 0 or num < min_num:
          min_num = num
  return min_num -2
_ = int(input())
print(cal(_))

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

if N % 2 == 0:
	yaku = 2
	
for i in range(3,int(math.sqrt(N))+1 ,1):
	if N % i == 0:
		yaku = i
		
		
yaku2 = int(N/yaku)

print(int(yaku2+yaku - 2))

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

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

Solution (Python):
from collections  import defaultdict
import bisect

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())
M=sorted(make_divisors(N))
p=bisect.bisect(M, N**0.5)

ans1=M[p]+(N//M[p])-2
ans2=9999999999999999999
ans3=9999999999999999999
if p>0:
    ans2=M[p-1]+(N//M[p-1])-2
if p<len(M)-1:
    ans3=M[p+1]+(N//M[p+1])-2
print(min(ans1,ans2,ans3))

Solution (Python):
n = int(input())
min = n+1
kaisu = int(n-1)

for i in range(kaisu):
    if n%(i+1)==0:
        tmp = i+(n/(i+1))-1
        if min > tmp:
            min = tmp
    if (i+1)**2>n:
        break

print(int(min))

Solution (Python):
import math

N = int(input())

ans = 10**13
flag = 0
nijou = math.sqrt(N)


for i in range(2,int(nijou +1e-9)+1):
    if N%i == 0:
        j = N//i
        
        if ans > (j-1)+(i-1):
            ans = (j-1)+(i-1)

        flag = 1

        
    else:
        continue

if flag == 0:
    ans = N-1

print(ans)

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

Solution (Python):
import math

n = int(input())

ns = [int(i) for i in str(n)]
a = 0
b = 0
ans = 0
f = 0


y = math.sqrt(n)
for i in range(int(y),1,-1):
    if n % i == 0:
        a = i
        b = n // i
        break
else:
    ans = n - 1
    f = 1















if f:
    print(ans)
else:
    ans = a - 1 + b - 1
    print(ans)

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

Solution (Python):
a = int(input())
min_steps=a
if a==2:
    print(1)
elif a==3:
    print(2)
else:
    j=2
    while(1):
        if j*j>=a:
            break
        else:
            j+=1

    for i in range(2, j+1):
            if a%i==0:
                b=i+int(a/i)-2
                if b<min_steps:
                    min_steps=b
    if (min_steps==a):
        min_steps-=1
    print(min_steps)

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

import math

k = int(math.sqrt(n))
t = n
q = 1

while k > 1:
    if n % k == 0:
        q = n//k
        t = int(k)
        break
    k -= 1

print(t+q-2)

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

n = int(input())						

a=int(n**(1/2))

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

print(i-1+j-1)

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

Solution (Python):
import math

n = int(input())
nr = int(math.sqrt(n))+1
ans = 0


for i in reversed(range(1,nr)):
    if n % i == 0:
        n //= i
        ans = i
        break


print(ans+n-2)

Solution (Python):

n = int(input())
a = 10**15


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


if a == 10**15:
    print(n-1)
else:
    print(a+b-2)

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

def divisor(n):
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(set(table))
    return table
 
if len(divisor(N)) < 3:
    print(N - 1)
else:
    divisor_list = divisor(N)
    tmp = N
    for i in divisor_list:
        A = i
        B = N / i
        if tmp > (A + B - 2):
            tmp = (A + B - 2)
    print(int(tmp))

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

n=int(input())
s=make_divisors(n)

if len(s)%2==0:
    k=int(len(s)/2) 
    ans=max((n/s[k]+s[k])-2,0)
    print(int(ans))
else:
    k=int(len(s)/2) 
    ans=min(n/s[k]+s[k]-2,n/s[k+1]+s[k+1]-2)
    print(int(ans))

Solution (Python):
import math
n = int(input())
flag = True
ans = n - 1
for i in reversed(range(2, int(math.sqrt(n))+1)):
  if (n%i == 0):
    ans = i + n//i - 2
    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 = 100000000000000
for i in a:
  if(ans > i+int(n/i)-2):ans = i+int(n/i)-2
print(ans)

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

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

n=int(input())
m=prime_factorize(n)
M=m[::-1]

if m==[n]:print(n-1)
else:
    a=int(n**(1/2))
    l=[]
    for i in range(1,a+1):
        if n%i==0:
            l.append(i)
    
    ans=(l[-1]-1)+(n//l[-1]-1)
    print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

def main():
    n = INT()
    lis = make_divisors(n)
    

    if len(lis) % 2 == 0:
        p = len(lis)//2
        print(lis[p-1] + lis[p] - 2)
    else:
        q = len(lis)//2
        print(lis[q] * 2 - 2)

if  __name__=='__main__':
    main()

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

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


divs = make_divisors(N)
if N // divs[len(divs) // 2] == divs[len(divs) // 2]:
    i = len(divs) // 2
else:
    i = len(divs) // 2 - 1
print(divs[i]-1 + N // divs[i]-1)

Solution (Python):
def divisor(n):
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    
    table = list(table)
    table.sort()
    return table

N = int(input())


n = []
n = divisor(N)


cnt = 999999999999999
for i in range(0,len(n)//2,1):
    t = int(n[i])-1 + int(n[len(n)-i-1])-1
    if cnt > t:
        cnt = t
print(cnt)

Solution (Python):
from math import sqrt, floor

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 main():
    N = int(input())

    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    

    NN = floor(sqrt(N))
    mn = N + 100
    for i in range(1, NN + 1):
        if N % i == 0:
            a = i
            b = N // i
            
            mn = min(mn, a + b)

    print(mn - 2)

if __name__ == "__main__":
    main()

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

Solution (Python):
import math
N = int(input())
i = 1
min = 9000000000000
for _ in range(1, int(math.sqrt(N)+1)):
  if N % i == 0:
    j = N/i
    sum = j + i
    if sum < min:
      min = sum
      ans = i-1+j-1
    else:
      break
  i += 1
print(int(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)
    divisors.sort()
    return divisors

key_list = make_divisors(n)
ans = 10000000000000
for value in key_list:
    key = value + n//value - 2
    if key < ans:
        ans = key
print(ans)

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

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


def solve(n):
    i = 1
    ans = N - 1
    while i * i <= n:
        if n % i == 0:
            ans_tmp = ((N // i) - 1) + (i - 1)
            if ans > ans_tmp:
                ans = ans_tmp
        i += 1
    return ans


print(solve(N))

Solution (Python):
import math

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

if n == 2:
    print(1)
    exit()

for i in range(1, r+1)[::-1]:
    if n % i == 0:
        print(i + int(n/i) - 2)
        exit()

Solution (Python):

import math

N = int(input())

left_side = 1
right_side = N


square = int(math.sqrt(N))
for i in reversed(range(1, square+1)):
    if N % i == 0:
        left_side = i
        right_side = N // i
        break

distance = (left_side - 1) + (right_side - 1)
print(distance)

Solution (Python):
import numpy as np
N=int(input())
a=int(np.sqrt(N))
for i in range(int(np.sqrt(N))):
    if N%a==0:
        print(a+N//a-2)
        break
    a-=1

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

yakusu = make_divisors(N)
l = len(yakusu)
if l % 2 == 0:
    i = yakusu[l//2-1]
    j = yakusu[l//2]
else:
    i = yakusu[l//2]
    j = yakusu[l//2]

ans = i + j - 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)
    divisors.sort()
    return divisors

kanousei = []

yakusuu_list = make_divisors(n)

for i in yakusuu_list[:-(-len(make_divisors(n))//2)]:
    if n%i==0:
        kanousei.append([i, int(n/i)])

saisyou = n + 1
index = 0
        
for i in kanousei:
    if sum(i) < saisyou:
        saisyou = sum(i)

print(saisyou-2)

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

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

Solution (Python):
from sys import stdin
import math

n = int(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)

    
    return divisors

li = make_divisors(n)

total = n+1

for i in range(len(li)):
    a = li[i]
    b = n//a
    total = min(total,a+b)

print(total-2)

Solution (Python):
import itertools
import collections

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 calc_dist(x, y):
	return abs(x - 1) + abs(y - 1)

n = int(input())
ll = prime_factorize(n)
c = collections.Counter(ll)
kk = list(c.keys())
vv = list(c.values())
pp = [list(range(vv[i] + 1)) for i in range(len(kk))]

min_dist = 10 ** 12



for bb in itertools.product(*pp):
	l = 1
	r = 1
	for i in range(len(kk)):
		l *= max(kk[i] ** bb[i], 1)
		r *= max(kk[i] ** (vv[i] - bb[i]), 1)
	
	min_dist = min(min_dist, calc_dist(l, r))
print(min_dist)

Solution (Python):
import sys
import math

N = int(sys.stdin.readline())

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

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

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

divisors = []

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

if y != 1:
	print((y-1)+(x-1))
else:
	print(x-1)

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

max = math.floor(N**0.5)
A = 1
B = 1
for i in range(max):
    if N % (max - i) == 0:
        A = (max - i)
        B = N // A
        break

print(A + B - 2)

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

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

Solution (Python):

import sys
input = sys.stdin.readline


def main():
    n = int(input())
    x = int(n**0.5)

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


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
f = 10**12

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

for i in make_divisors(n):
    z = i + n//i - 2
    
    if z < f:
        f = z

print(f)

Solution (Python):

n = int(input())

def make_divisor(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

vals = make_divisor(n)
result = n - 1
for val in vals:
    if val == 1 or val == n:
        continue
    result = min(result, int(n/val)+val - 2)

print(result)

Solution (Python):

import math
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

def findRoot(N):
    root = round(math.sqrt(N+1))
    return root

def main():
    N=int(input())  
    for i in list(reversed(range(1, findRoot(N)+1))):
        a, b = divmod(N, i)
        if b == 0:
            c = N // a
            print(a+c-2)
            break
            

if __name__ == '__main__':
    main()

Solution (Python):
import math
n = int(input())
x = 1
y = 1
N = int(math.sqrt(n))
for i in range(N):
    if i == 0:
        X = n-i
        Y = 1
    if n % (i+1) == 0:
        x = i+1
        y = n // (i+1)
        if (X+Y) > (x+y):
            X = x
            Y = y
print(X + Y - 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)
            divisors.append(n//i)

    return divisors

N = int(input())

combo = make_divisors(N)
sum = [sum(combo[i:i+2]) for i in range(0,len(combo),2)]
minimum = min(sum) - 2

print(minimum)

Solution (Python):
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append((i,n//i))
    return divisors
    
n = I()
p = make_divisors(n)
ans = n
for i in range(len(p)):
    ans = min(ans, p[i][0] + p[i][1] -2)
print(ans)

Solution (Python):
import math

def main():
    Input = [input() for i in range(1)]
    N = int(Input[0])
    x = math.sqrt(N)
    if x.is_integer():
        print(int(x+N/x-2))
    else:
        while x >= 1:
            x = math.ceil(x)-1
            y = N/x
            if y.is_integer():
                print(int(x+y-2))
                break

        
        
if __name__ == "__main__":
    main()

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

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

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

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

print(min(divisors)-2)

Solution (Python):



import pdb
import sys
import math
F = sys.stdin
N = int(F.readline())
sqN = int(math.sqrt(N))
ans = 0

for i in range(1, sqN+1):
    if N % i == 0:
        A = int(i - 1)
        B = int((N / i) - 1)
        if ans == 0 or A + B < ans:
            ans = A + B

print(ans)

Solution (Python):
import sys
import os
import itertools

n = int(input())

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

    return divisors

ans = make_divisors(n)
m = max(ans)
print( (m-1) + ans[m]-1)

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())
p = make_divisors(n)
print(min([a+n//a-2 for a in p]))

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

Solution (Python):
import math
A = int(input())
p = int(math.sqrt(A)) + 1
all_lis = []
ans_lis = []

for i in range(1,p,1):
    if A % i == 0:
        kumi = [i,int(A/i)]
        all_lis.append(kumi)

for j in all_lis:
    ans_lis.append(j[0]+j[1])
    
print(int(min(ans_lis)-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((n//i, i))
            if i != n // i:
                divisors.append((n//i, i))
    return set(divisors)


pairs = list(make_divisors(n))
min_pair = n-1

for p in pairs:
    result = p[0] + p[1] -2
    if result < min_pair:
        min_pair = result

print(min_pair)

Solution (Python):
import math

N = int(input())

a = int(math.sqrt(N))
while a > 1:
	if N % a == 0:
		break
	a -= 1
b = N // a
print(a+b-2)

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

fac=1
s=int(n**(1/2)+1)
for i in range(s):
    if n%(s-i)==0:
        fac=s-i
        break

ans=fac+(n//fac)-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)

    
    return divisors

N=int(input())
L=make_divisors(N)

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

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

Solution (Python):
import math
s=input()
b=0
c=0
a=int(s)
d=int(math.sqrt(a))
for i in range(1,d+1,1):
  if a%i==0:
    b=i
    c=a/i
e=int(b-1+c-1)
print(e)

Solution (Python):
N =int(input())
ans = 0
L =[]
for i in range(2,1000001):
    if N % i == 0:
        M = N/i
        L.append(M+i)
        ans += 1
if ans == 0:
    print(N-1)
else:
    print(round(min(L)-2))

Solution (Python):
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy




'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  
print(list(set(a) & set(b)))  
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.extend(要素)
.insert(場所,要素)
del 配列名[場所]
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
'''
copy.copy(配列名)
'''
'''
sort()50
sorted(reverse=true)
'''
'''
2分探索
bisect.bisect_left(リスト, 要素)
'''


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


num = int(stdin.readline().rstrip())
yaku = make_divisors(num)
yaku.sort()

res = []
if (int(len(yaku)) % 2 == 0):
    res.append(yaku[int(len(yaku) / 2 - 1)])
    res.append(yaku[int((len(yaku)) / 2)])
    print(int(res[0]-1)+int(res[1]-1))
else:
    res.append(yaku[int((len(yaku)) / 2)])
    print(int(res[0]-1)+int(res[0]-1))

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

main()