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())
s = N
i = 1
while i**2 <= N:
    if N % i == 0:
        s = i
    i += 1

ans = N//s + s - 2
print(ans)

Solution (Python):
N = int(input())
root = int(N ** (1/2)) + 1
num = 1
for i in range(root, 0, -1):
    if N % i == 0:
        num = i
        break
num2 = N // num
print(num+num2-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)

    
    return divisors

a = make_divisors(n)
a.sort()

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

Solution (Python):
import sys

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

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

Solution (Python):
import math

N = int(input())

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

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


def divisor(n):
    a = []
    root_n = int(n**0.5)
    for i in range(root_n):
        if n % (i+1) == 0:
            a.append(i+1)
            if (i+1) != n // (i+1):
                a.append(n//(i+1))
    a.sort()
    return a

yaku = divisor(n)
ima = n + 2
for i in yaku:
    othe = n // i
    ima = min(ima,i + othe)
print(ima - 2)

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

f=True
for i in range(int(math.sqrt(N))):
    n=int(math.sqrt(N))-i
    if N%n==0:
        print(int(n+N/n-2))
        f=False
        break
if f:
    print(N-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

import collections
import numpy as np







N = int(input())

yakusu = make_divisors(N)
yakusu.sort()
ans = np.inf
for y in yakusu:
  if y + N // y < ans:
    ans = (y + N // y) - 2

print(ans)

Solution (Python):
def c_walk_on_multiplication_table():
    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))
    return ans

print(c_walk_on_multiplication_table())

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

def get_divisors(n, reverse=False):
    from math import sqrt
    divisors = []
    for num in range(1, int(sqrt(n))+1):
        if n % num == 0:
            divisors.append(num)
            divisors.append(n//num)
    return sorted(divisors, reverse=reverse)

divisors = get_divisors(N)

l = len(divisors)
ans = float('inf')
for i in range(l//2):
    ans = min(ans, divisors[i] + divisors[l - 1 - i])
print(ans-2)

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

n = int(input())
ans_list = []
for i in range(1,10**6+1):
    if n % i == 0:
        ans_list.append(n//i+i)

print(min(ans_list)-2)

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


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


ans = 10 ** 12 + 9
x, y = modif(n)
for i, j in zip(x, y):
    ans = min(ans, i + j - 2)
print(ans)

Solution (Python):
n=int(input())
ans=999999999999
for i in range(1,1000001):
    if n/i==n//i:
        if ans>i-1+n//i-1:
            ans=i-1+n//i-1
print(ans)

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

Solution (Python):
import math
N=int(input())
flag=False
ls=[]
mi=N-1
for i in range(int(math.sqrt(N))+2):
  if N%(i+1)==0:
    mi=min([mi,(i+1)+(N//(i+1))-2])
print(mi)

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

l = make_divisors(N)
ll = [(v1 - 1) + (N / v1 - 1) for v1 in l]
print(int(min(ll)))

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

Solution (Python):

n = int(input())
index = 2
result = 10**13
while index * index <= n:
    if n % index == 0:
        curr = (n // index) - 1
        curr += (index - 1)
        result = min(curr, result)
    index += 1
if result == 10**13:
    result = n - 1
print(result)

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

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

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

Solution (Python):
N = int(input())
INF = 10**15
m = int(N**.5)+1

ans = INF
for i in range(1,m):
    if N%i == 0:
        t = i + N//i
        if  t < ans:
            ans = t
print(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,n // i])
    
    return divisors

n = int(input())
a = make_divisors(n)
print(a[-1][0]+a[-1][1]-2)

Solution (Python):
def make_divisors(n):
    for i in range(int(n**0.5)+1,0,-1):
        if n % i == 0:
            return i
            break


m = int(input())


k = make_divisors(m)
j = m / k

print(int((k-1)+(j-1)))

Solution (Python):
import sys


def main():
    n = int(sys.stdin.readline().strip())
    

    dividers = [1]
    for d in range(2, int(n ** 0.5)+1):
        if n % d == 0:
            dividers.append((d))

    divider = dividers[-1]
    ans = (n // divider - 1) + (divider - 1)
    print(ans)


main()

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

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

Solution (Python):
import sys, re, os
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
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
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
l = make_divisors(N)
r = sorted(l)
l = sorted(l, reverse = True)



s = []
for i in range(len(r)):
  s.append(r[i]+l[i])

print(min(s)-2)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
from math import factorial, ceil, floor
from bisect import bisect_right as bsr
from operator import itemgetter as ig
from collections import defaultdict as dd
from collections import deque, Counter as cnt


args = None
INF = float("inf")
MOD = int(1e9 + 7)
def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]
def nlist(n, v):
    if not n: return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def main():
    N = input(int)
    ans = N
    for n in range(int(N ** 0.5), 0, -1):
        if N % n == 0:
            temp = n + N // n
            if temp < ans:
                ans = temp
    if ans == N:
        print(ans - 1)
    else:
        print(ans - 2)
    return

if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Solution (Python):




import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())



ans = n+100
for i in range(1,1000001):
    if n%i == 0:
        c = i + n//i -2
        if c < ans: ans = c
    

print(ans)

Solution (Python):
import math
N=int(input())
n=math.floor(math.sqrt(N))
check=True
for i in range(n,0,-1):
  if N%i==0:
    x=N/i
    y=i
    break
  else:
    x=1
    y=N
print(math.floor(x+y-2))

Solution (Python):
import math
N = int(input())
nn = math.ceil(math.sqrt(N))
ans = 10e+13
for i in range(1, nn+1):
    if N % i == 0:
        tmp = N // i
        ans = min(ans, i + tmp -2)
print(ans)

Solution (Python):
N = int(input())
a = int(N**(1/2)) + 1
if a*a > N:
    a -= 1
while(a>0):
    if N%a==0:
        break
    a -= 1

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

Solution (Python):

import sys
INF = float("inf")

import math
import collections


def divisors_kai(n):
    ret = 1
    for i in range(1, int(n ** 0.5) + 1):
        d, m = divmod(n, i)
        if m == 0:
            ret = i
            
    return ret


def solve(N: int):
    a = divisors_kai(N)
    b = N/a
    print(int(a + b - 2))
    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):
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())
ans_li = []
divisors = make_divisors(n)
for item in divisors:
  a = item + n//item
  ans_li.append(a)
print(min(ans_li)-2)

Solution (Python):
import numpy as np
a, = [list(map(int, s.split())) for s in open(0)][0]
min_ = a - 1
for i in range(2,int(np.sqrt(a)+1)):
    if a % i == 0:
        tmp = i + a//i - 2
        if tmp < min_:
            min_ = tmp
print(min_)

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())
dv1 = make_divisors(N)
dv2 = sorted(dv1, reverse=True)
dv = min([dv1[i]+dv2[i] for i in range(len(dv1))])
print(dv-2)

Solution (Python):
import numpy as np

N = int(input().strip())

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

Solution (Python):
def main():
    N = int(input())
    imax = int(N**0.5)
    for i in range(imax, 0, -1):
        if N % i == 0:
            j = N//i
            print(i + j - 2)
            return
    print('No')
    return

main()

Solution (Python):
import math

N = int(input())

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

print(tmp-1+N//tmp-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())
nums = make_divisors(N)
if len(nums) %2 == 1:
  print((nums[-1]-1)*2)
  exit()
else:
  print(nums[-1]-1+nums[-2]-1)

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()

import numpy as np
start = int(np.sqrt(N))
while N % start != 0:
    start -= 1
a, b = start, N//start
print(a+b-2)

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

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

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())

    n = int(N ** 0.5)
    for i in range(n, 0, -1):
        d, m = divmod(N, i)
        if not m:
            print(d + i - 2)
            break


main()

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

print(int(r))

Solution (Python):
import math as m


def calc(N):
    for i in range(m.floor(m.sqrt(N)), 0, -1):
        if N % i == 0:
            return (i - 1) + (N // i - 1)


N = int(input())
print(calc(N))

Solution (Python):
import numpy as np

n = int(input())

num = int(np.sqrt(n) // 1)

ans = 0
for i in range(1, num+1):
    if n % i == 0:
        ans = i

print(int(n/ans + ans - 2))

Solution (Python):
import math

n = int(input())

k = int(math.sqrt(n))
for i in range(k):
    a = k-i
    if n % a == 0:
        print(a -2 +n//a)
        break
    else:
        pass

Solution (Python):



import math


def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True


def main():

    n=int(input())

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






if __name__ == "__main__":
    main()

Solution (Python):


A=list(map(int,(input().split())))
N=A[0]
def dou(ans):
	for x in range(int(N**0.5+1),0,-1):
		if ans%x==0:
			return x
ans=dou(N)
print(N//ans+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())
divide = make_divisors(N)
divide.sort()
LEN = len(divide)
LENG = LEN//2

ans = divide[LENG]
an = N//ans
print(ans-1+an-1)

Solution (Python):
from bisect import bisect_left
import math
def divisors(n, sort=True, reverse=False):
    sqn = math.sqrt(n)
    sqn_int = int(sqn)
    div_list = []
    if sqn == sqn_int:
        div_list.append(sqn_int)
        loop = range(1,sqn_int)
    else:
        loop = range(1,sqn_int + 1)
    for i in loop:
        if n % i == 0:
            div_list.append(i)
            div_list.append(n//i)
    if sort:
        div_list.sort(reverse=reverse)
    return div_list

N = int(input())
div_list = divisors(N)
i = bisect_left(div_list, int(math.sqrt(N)))
a = div_list[i]
b = N//a
print(a+b-2)

Solution (Python):
def f(N):
  output=[]
  temp=int(pow(N, 1/2))+1
  for i in range(1, temp):
    if N%i==0:
      j=N//i
      output.append([i, j])
      
  return output
    

N=int(input())
nums=f(N)

temp=len(nums)
cand=[]
for temp in nums:
  cand.append(temp[0]+temp[1]-2)
  
print(min(cand))

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

Solution (Python):
from math import sqrt

n = int(input())
max_div = int(sqrt(n))

for d in range(max_div, 0, -1):
  if n % d == 0:
    print(d + n // d - 2)
    break

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

Solution (Python):


N=int(input())

i=1
minStep=0
while i*i<=N:
    if N%i==0:
        minStep=i+(N//i)
    i+=1
print(minStep-2)

Solution (Python):
import sys
sys.setrecursionlimit(int(1e6))

N = int(input())

import math
a = int(math.sqrt(N))
while (a > 1):
    if (N % a) == 0:
        break
    a -= 1

b = N // a
ans = a - 1 + b - 1
print(ans)

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

Solution (Python):
import math

n = int(input())

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

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split()) 
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
mn = 1234567890123
for i in range(1,10**6+1):
  if n%i > 0:
    continue
  if i + n//i < mn:
    mn = i + n//i
print(mn-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


table=divisor(N)
tmp=1+N
for i in table:
    tmp=min(tmp,i+N//i)

print(tmp-2)

Solution (Python):
def main():
    n=int(input())
    ans = n-1
    for i in range(1,int(n**0.5//1+1)):
        if n%i==0:
            ans = min(ans,n//i+i-2)
    print(ans)
if __name__ == '__main__':
    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


ans_list = make_divisors(N)

_len = len(ans_list)




if _len % 2 == 0:
    print(ans_list[-1]+ans_list[-2]-2)
else:
    print(ans_list[-1]*2-2)

Solution (Python):
def yaku(n):
    l=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            l+=[(i,n//i)]
    return l
        

def resolve():
    n=int(input())
    l=yaku(n)
    ans=[i[0]+i[1]-2 for i in l]
    print(min(ans))
resolve()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    N=ii()
    x=int(N**0.5)
    while x>0:
        y=N//x
        if x*y==N:
            print(x+y-2)
            exit()
        else:x-=1


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
dmin = 10**13
for i in range(1,int(N**0.5)+1):
    if N%i==0:
        j = N//i
        dmin = min(dmin,abs(i-1)+abs(j-1))
print(dmin)

Solution (Python):
import math


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

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

Solution (Python):
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
N= int(input())
M = int(N**(1/2))
ans = 1
for i in range(1,M+1):
    if N%i == 0:
        ans = i
print(ans+N//ans-2)

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

Solution (Python):
import math


def solve_c(c):
    res = c - 1
    d = math.floor(math.sqrt(c))
    for a in range(2, d + 1):
        if c % a == 0:
            res = (a - 1) + (c // a - 1)
    return res


def main():
    
    c = int(input())
    res = solve_c(c)
    print(res)


def test():
    assert solve_c(10) == 5
    assert solve_c(50) == 13
    assert solve_c(10000000019) == 10000000018


if __name__ == "__main__":
    test()
    main()

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

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

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def divisor(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            yield i
            yield n // i

@mt
def slv(N):
    d = [n for n in divisor(N)]
    x = d.pop()
    y = d.pop()
    return (x-1) + (y-1)


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Solution (Python):
import math

n = int(input())
q = int(math.sqrt(n))
result = n - 1

for i in range(q):
    c = q - i
    if n % c == 0:
        result = c + n // c - 2
        break

print(result)

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


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


def main():
    N = int(input())
    D = make_divisors(N)
    mi = N
    for d in D:
        mi = min(mi, d + (N//d) - 2)
    print(mi)


if __name__ == '__main__':
    main()

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

rt = int(N**0.5)

for i in range(rt, 0, -1):
    if N % i == 0:
        j = N//i
        i = i-1
        j = j-1
        print((i+j))
        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

divisors = make_divisors(N)

min_sum = 0

for A in divisors:
    B = N // A
    if min_sum == 0 or A+B <= min_sum:
        min_sum = A+B
print(min_sum-2)

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

Solution (Python):
def calc_divisore(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())
d=calc_divisore(n)
if len(d)%2==0:
    print((d[len(d)//2]+d[(len(d)//2)-1]-2))
else:
    print((d[len(d)//2]*2)-2)

Solution (Python):
import math

def solve():
  N = int(input())
  for i in range(math.floor(math.sqrt(N)), 0, -1):
    if N % i != 0: continue
    return i + (N // i) - 2

print(solve())

Solution (Python):
from math import sqrt,ceil

N = int(input())

for x in reversed(range(ceil(sqrt(N))+1)):
    if N%x == 0:
        print(x+N//x-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:
        print(i+N//i -2)
        exit()

print(N-1)

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

Solution (Python):

import sys
from math import sqrt

n= int(sys.stdin.readline().strip())
ans = n-1
for i in range(int(sqrt(n)),0,-1):
    if n%i==0:
        ans = i + n//i - 2
        break

print(ans)

Solution (Python):


from math import sqrt, floor

def readInts():
  return [int(s) for s in input().split(" ")]

def solve(N):
  ans = N - 1
  for i in range(1, floor(sqrt(N)) + 1):
    if N % i == 0:
      ans = min(ans, (i - 1) + ((N // i) - 1))
  return ans

def main():
  N = readInts()[0]
  print(solve(N))

if __name__ == "__main__":
  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

m = 10**12
divs = make_divisors(N)
for d1 in divs:
    d2 = N // d1
    m = min(m, d1+d2-2)
print(m)

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

Solution (Python):

N = int(input())

ans = []

for i in range(1,10**6+1):
  if N % i == 0:
    ans += [i + N//i - 2]

print(min(ans))

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

ans = float('inf')
for i in range(1, int(N ** 0.5) + 1):
    if N % i == 0:
        ans = min(ans, i + N//i)

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

    
    return divisors


N = int(input())
a = make_divisors(N)[-1]
b = N // a
print(a + b - 2)

Solution (Python):
n = int(input())
a = int(n**0.5)+1
for v in range(a,0,-1):
    q,r = divmod(n,v)
    if r==0:
        print(q+v-2)
        break

Solution (Python):
def main():
    n = int(input())
    
    d_s = make_divisors(n)

    ans = n - 1

    for d in d_s:
        ans = min(ans, (d-1) + ((n // d) - 1))
    print(ans)


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

if __name__ == '__main__':
    main()

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

import math

root=int(math.sqrt(n))
ans=float("inf")

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

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


def m(n):
    res = []
    m = 1000000000010
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:


            s = i + n//i

            m = min(s,m)

    
    return m
print(m(a)-2)

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

ans=10**12
for i in range(1,N):
  if i>10**6:
    break
  
  if N%i==0:
    A=i
    B=N//i
    if A+B-2<ans:
      ans=A+B-2
      
print(ans)

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

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

Solution (Python):
import math
N = int(input())
ans = 1000000000000000000
sqN = int(math.sqrt(N))+1
for i in range(1,sqN):
    if N%i == 0:
        value = N//i + i - 2
        if ans > value:
            ans = value
print(ans)

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

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7

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 main():
    n = int(input())
    ans = float("inf")
    for i in range(1,1000001):
        if n % i == 0:
            ans = min(i-1 + n//i-1,ans)
    print(ans)





    
            
    

    

    

if __name__ == '__main__':
    main()

Solution (Python):
def fact(n):
    l=set()
    x=1
    while(x*x<=n):
        if(n%x==0):
            l.add(x)
            l.add(n//x)
        x+=1
    l=list(l)
    l.sort()
    return l
n=int(input())
l=fact(n)

ans=10**12
i=0
j=len(l)-1
while(i<j):
    ans=min(ans,l[i]-1+l[j]-1)
    i+=1
    j-=1
if(len(l)%2==1):
    ans=min(ans,l[len(l)//2]-1+l[len(l)//2]-1)
print(ans)

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

    
    ans = 10**13
    for i in range(1, int(n**0.5)+1):
        num = n / i
        if num - int(num) == 0:
            ans = min(ans, i+num-2)
    print(int(ans))


main()

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())


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 = readi()
mn = -1
for i in make_divisors(n):
    p = i - 2
    t = n // i
    p += t
    if mn == -1:
        mn = p
    mn = min(mn, p)
print(mn)

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

Solution (Python):

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