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())
import math
divisors = []
for i in range(1, math.ceil(math.sqrt(n))+1):
  if n%i==0:
    divisors.append([i, n//i])

ans = 2*10**12
for divisor in divisors:
  ans = min(ans, divisor[0]+divisor[1]-2)
print(ans)

Solution (Python):
import math
N = int(input())
n = int(math.ceil(math.sqrt(N)))
ans = 0
for i in range(n,0,-1):
    if N % i == 0:
        j =int(N / i)
        break
print(i+j-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


def nearN():
    if N_diviors_len == 2:
        print(N - 1)
        exit()

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


N = int(input())
N_diviors = make_divisors(N)
N_diviors_len = len(N_diviors)
N_sqrt = math.sqrt(N)

nearN()

Solution (Python):
import math
N = int(input())
Min = float('inf')
Max = math.ceil(math.sqrt(N))
for i in range(1,Max+1):
    if N%i==0:
        b = N//i
        Min = min(Min,i+b-2)
print(Min)

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

i = 1
ans = 1e16

while i <= math.sqrt(n):
  b = divmod(n,i)
  if b[1] == 0:
    tmp = b[0] + n // b[0] - 2
    if tmp < ans:
      ans = tmp
  i+=1
   
print(ans)

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

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

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

print(int(ans))

Solution (Python):
N = int(input())
ans = 10**13
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):
N=int(input())

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

print(a+b-2)

Solution (Python):
N = int(input())
import math
sqrtN = math.floor(math.sqrt(N))
list = range(1, sqrtN+1)
a=[]
for i in list[::-1]:
    while N%i == 0:
        a.append(int(i))
        a.append(int(N/i))
        break
    if not a:
        pass
    else:
        break
f = a[0]+a[1]-2
print(f)

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

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

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

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

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

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def divisors(n, sort=False):
    """
    nの約数をO(√n)で列挙する
    """
    d = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            d.append(i)
            if i != n // i:
                d.append(n//i)
    if sort:
        d.sort()
    return d


N = int(input())

ans = float('inf')
d = divisors(N, sort=True)
for i in range(len(d)):
    x, y = d[i], d[-i - 1]
    dist = abs(x - 1) + abs(y - 1)
    ans = min(ans, dist)
print(ans)

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

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

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

N = int(input())


def main():

    end = N ** 0.5
    minv = 10 ** 12
    list = []
    for i in range(1, int(end) + 1):
        if N % i == 0:
            list.append([i, N / i])
    for item in list:
        minv = min(minv, item[0] + item[1] - 2)

    print(int(minv))

if __name__ == '__main__':
    main()

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

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

ans = float('inf')
for a in range(1, int(n**(1/2)) + 1):
    if n % a != 0:
        continue
    b = n // a
    ans = min(ans, a+b-2)
print(ans)

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

print(sum(p) - 2)

Solution (Python):

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
import math
from heapq import heappush, heappop, heapify


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

    v = 1
    while v * v <= n:
        v += 1

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


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
ans = float('inf')
for i in range(1, 10**6 + 1):
  if N % i != 0:
    continue
  j = N // i
  ans = min(ans, i + j - 2)
print(ans)

Solution (Python):
N = int(input())
res = int(1e13)

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

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

Solution (Python):
N=int(input())
P=[1,N]

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

print(sum(P)-2)

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

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

Solution (Python):
import math


N = int(input())

floorNsqrt = int(math.sqrt(N))

i = 0
while i <= floorNsqrt:
    i += 1
    if N % i == 0:
        N_a = i

N_b = N / N_a
ans = int(N_a-1 + N_b-1)


print(ans)

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

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

print(m - 2)

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

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

Solution (Python):

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

if __name__ == "__main__":
    n = int(input())
    ret = make_divisors(n)
    
    ans = 10**18
    for a,b in ret:
        ans = min(a+b, ans)
    print(ans-2)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 




 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    
    if is_prime(N):
        print(N-1)
        sys.exit()
        
    ll = factorize(N)
    ll.sort()
    
    if len(ll) == 2:
        a = ll[0] -1
        b = ll[1] -1
        print(a + b)
        sys.exit()
        
    tmp = math.sqrt(N)
    max = (tmp // 1) + 1
    ans = INF
    for i in range(1, int(max)):
        b = N / i
        if b.is_integer():
            tmp = (i-1) + (b-1)
            if ans > tmp:
                ans = tmp
                
    print(int(ans))
    
def is_prime(n): 
    i = 2
    while i * i <=n:
        if n % i == 0:
            return False
        i += 1
    return True
    
def factorize(n):
    b = 2
    fct = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            fct.append(b)
        b = b + 1
    if n > 1:
        fct.append(n)
    return fct
    
if __name__ == "__main__":
	main()

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

U = int(np.sqrt(N)) + 1
div = np.arange(1, U, dtype=np.int64)
div = div[N%div == 0]
div += N//div
print(np.amin(div) - 2)

Solution (Python):
import math

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

candidate = N - 1

for i in range(2, sqrtN+1):
    if not N % i:
        if N // i + i - 2 < candidate:
            candidate = N // i + i - 2
print(candidate)

Solution (Python):
import numpy as np
import math

n = int(input())
x = []
for i in range(1,math.floor(math.sqrt(n))+1):
    if(n%i == 0):
        x.append(i+n/i-2-0)
print(round(min(x)))

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

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

print(ans)

Solution (Python):
import math

n = int(input())
x = int(math.sqrt(n)) + 1

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

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

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

Solution (Python):
import math

n = int(input())
ans = n-1

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

Solution (Python):
import math
n = int(input())
maxn = math.ceil(math.sqrt(n))
res1=1
for i in range(2,maxn+1):
    if n % i == 0:
        res1 = i

print(res1 + (n // res1)-2)

Solution (Python):

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

Solution (Python):

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

Solution (Python):
import math

n = int(input())
m = math.floor(math.sqrt(n))
flag = True

while flag:
  if n%m == 0:
    flag = False
    print(m + n//m -2)
  m -= 1

Solution (Python):
import math

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

Solution (Python):


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

Solution (Python):
import math

N = int(input())
N2 = int(math.sqrt(N))
ans = 0
for i in range(1, N2 + 1):
    j = int(N/i)
    if (i * j == N):

        ans = i + j - 2


print(ans)

Solution (Python):
import math

N = int(input())

sqrtN = int(math.sqrt(N))

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

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())
d = make_divisors(n)
d.sort()

l = len(d)
print(d[l//2 - 1] + d[l//2] - 2)

Solution (Python):
import math
import numpy as np
N = int(input())
n = np.arange(1,math.sqrt(N)+1)
print(int(min(np.where(N%n == 0, (N/n)+n-2, np.nan))))

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

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

Solution (Python):

import sys
import numpy as np
import math


def solve(N: int):
    x = np.arange(1, np.sqrt(N)+1, dtype=np.int)
    div = x[N % x == 0]
    print((N/div + div - 2).min().astype(np.int))

    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):
import math
n = int(input())

result = float("INF")

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

Solution (Python):
n = int(input())
val = int(n**0.5)
while n % val != 0:
    val -= 1
print(val + n // val - 2)

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

Solution (Python):
import math

def sq(n):
    for i in range(math.ceil(math.sqrt(N)), -1, -1):
        if n % i == 0:
            return (i, n//i)

N = int(input())
a, b = sq(N)
print(a + b - 2)

Solution (Python):
import math

def sq(n):
    for i in range(math.floor(math.sqrt(N)), 0, -1):
        if n % i == 0:
            return (i, n//i)

N = int(input())
a, b = sq(N)
print(a + b - 2)

Solution (Python):
import math
n=int(input())
ans=n+1
i=1
while i<=math.sqrt(n):
    if n%i==0 and ans > i +n/i:
        ans = i + n/i
    i+=1
ans = int(ans)
print(ans-2)

Solution (Python):
import math

def sq(n):
    for i in range(math.floor(math.sqrt(N)), -1, -1):
        if n % i == 0:
            return (i, n//i)

N = int(input())
a, b = sq(N)
print(a + b - 2)

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


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

Solution (Python):
import math

n=int(input())

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

Solution (Python):
import math
def divisor(x):
    div=[]
    for i in range(1,int(x**(1/2))+1):
        if x%i==0:
            div.append(i)
            if i!=x//i:
                div.append(x//i)
    return sorted(div)

N=int(input())
aa=divisor(N)
bb=[]
for i in range(math.ceil(len(aa)/2)):
    bb.append(aa[i]**2+aa[len(aa)-i-1]**2)
m=bb.index(min(bb))
n=aa[m]-1
r=aa[len(aa)-1-m]-1
print(n+r)

Solution (Python):

def min_div_pairs_sum(x: int) -> int:
    ret = 1 << 60
    for i in range(1, int(x ** 0.5) + 1):
        if x % i == 0:
            ret = min(ret, i + x // i)
    return ret


def main():
    N = int(input())
    ans = min_div_pairs_sum(N) - 2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def div_pairs_sum(x: int) -> set:
    divs = set()
    for i in range(1, int(x ** 0.5) + 1):
        if x % i == 0:
            divs.add(i + x // i)
    return divs


def main():
    N = int(input())
    ans = min(div_pairs_sum(N)) - 2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def divisor(N):
    div = []
    for i in range(1, int(N ** 0.5 + 1)):
        if N % i == 0:
            div.append(i)
            if i * i != N:
                div.append(N // i)
    return sorted(div)


N = int(input())
print(min([d + N // d - 2 for d in divisor(N)]))

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

Solution (Python):
n = int(input())
ln = []
for k in range(1,(int(n**0.5))+1):
    if n % k == 0:
        ln.append(k)
if not n**0.5 % 1 == 0:
    for k in reversed(range(0, len(ln))):
        ln.append(n // ln[k])
else:
    for k in reversed(range(0, len(ln)-1)):
        ln.append(n // ln[k])
if not n**0.5 % 1 == 0:
    i = ln[len(ln) // 2 - 1]
    j = ln[len(ln) // 2]
    ans = i + j -2
else:
    ans = int(2*n**0.5)-2
print(ans)

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

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

Solution (Python):
from collections import Counter
import math
N = int(input())

L = math.sqrt(N)

now = 0
for i in range(1, int(L)+1):
    

    if N % i == 0:
        tmp = i + N / i -2
        if now == 0:
            now = tmp
        elif now >= tmp:
            now = tmp
        
        
        
print(int(now))

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

for i in range(int(n ** (1 / 2))):
    if i > n:
        break
    elif n % (i + 1) == 0:
        j = int(n / (i + 1)) 
        s.append(i + j - 1)

s.sort()                
print(s[0])

Solution (Python):
s=[]
n=int(input())
a=int(n**0.5)
for i in range(a):
    if n%(a-i)==0:
        s.append(a-i)
    if len(s)>=1:
        break
b=n//s[0]
print(b+s[0]-2)

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

r = int(math.sqrt(n))

a = 1
b = n

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

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

print(c_walk_on_multiplication_table())

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):
n = int(input())
a = int(n**(1/2))


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

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

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

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

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

Solution (Python):

from math import sqrt
N = int(input())

ans = N
MAX = int(sqrt(N)) + 1
for a in range(1, MAX):
    b = N % a
    if b == 0:
        ans = min(ans, a + N / a - 2)
        
print(int(ans))

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

Solution (Python):
N=int(input())
div=[]

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

div=list(map(int,div))
div.sort()
num=len(div)//2

x=div[num]
y=div[num-1]

print(x+y-2)

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

Solution (Python):


from math import sqrt
N = int(input())
n = int( N ** 0.5 )
ans = 0


for i in range(1,n+1):
    
    if N % i == 0:
        
      if ans == 0:
        ans = i + N/i
      else:
        ans = min(ans, i + N/i)
        

print(int(ans -2))

Solution (Python):
import math

n = int(input())

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

print(ans)

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

x=N

import math
xr=math.ceil(math.sqrt(x))

LIST=[]
for i in range(1,xr+1):
    if x%i==0:
        LIST.append(i)
        LIST.append(x//i)

ANS=1<<60

for l in LIST:
    ANS=min(ANS,l-1+N//l-1)

print(ANS)

Solution (Python):
A = int(input())
def div(n):
  for i in reversed(range(1, int(n**0.5)+1)):
    if n % i == 0:
      return (i, n//i)
def move(n):
  i, j = div(n)
  return i + j - 2
print(move(A))

Solution (Python):
A = int(input())
def getIJ(A):
  for i in reversed(range(1, int(A**0.5)+1)):
    if A % i == 0:
      return (i, A//i)
  return (0, 0)
def calc(A):
  i, j = getIJ(A)
  return i + j - 2
print(calc(A))

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

Solution (Python):
import sys
from collections import deque
import math



n = int(input())

dists = []
h = math.ceil(math.sqrt(n))

for i in range(1,h + 1):
    if n % i != 0:
      continue
    j = int(n / i)
    dists.append(i + j - 2)
print(min(dists))

Solution (Python):



import unittest
from io import StringIO
import sys
import math


def input():
    return sys.stdin.readline().rstrip()


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

    S = math.ceil(math.sqrt(N))
    ans = N - 1

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


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[: -1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """10"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """50"""
        output = """13"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10000000019"""
        output = """10000000018"""
        self.assertIO(input, output)


if __name__ == "__main__":
    
    resolve()

Solution (Python):
import numpy as np

N=int(input())

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

print(int(ans))

Solution (Python):
"""
author : dokueki
"""
import math
n = int(input())
res = []
for i in range(1,int(pow(n,0.5))+1):
    if (n/i)-math.floor(n/i)==0 :
        res.append((i+(n//i))-2)
print(min(res))

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

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

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

Solution (Python):
import sys
import math


def sp(value):
		ret_v=[]
		v=value
		for i in range(2, value):
			if v % i == 0:
				ret_v.append(i)
				v=v//i
				
				while v % i == 0:
					ret_v.append(i)
					v=v//i
					
			if i*i>v:
				ret_v.append(v)
				return (ret_v)
				
		ret_v.append(v)
		return (ret_v)

def main():
	a = int(input())
	mm=a*2
	for i in range(1, int(math.sqrt(a))+1):
		if a % i == 0:
			dd=i+(a//i)-2
			mm=min(dd,mm)

	print(mm)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
N = int(input())
S = N+1
from math import sqrt
for a in range(int(sqrt(N))+2, 0, -1):
    b, y = divmod(N, a)
    if y != 0:
        continue
    S = a + b
    break
print(S-2)

Solution (Python):
N = int(input())
S = N+1
from math import sqrt
for a in range(int(sqrt(N))+2, 0, -1):
    b, y = divmod(N, a)
    if y != 0:
        continue
    S = a + b
    break
print(S-2)

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

ans = 1000000
ans_list = []

for i in range(1, n):
    if N % i == 0:
        ans = N / i
        ans_list.append(i + ans)

print(int(min(ans_list)-2))

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

a = int(math.sqrt(num))
b = num // a

res_list = []

for i in range(1, a+1)[::-1]:
    b = num // i
    if i * b == num:
        print(i + b -2)
        break

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

Solution (Python):
n=int(input())
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):
import math 
num = int(input())

a = int(math.sqrt(num))
b = num // a

res_list = []
for i in range(1, a +1):
    b = num // i
    if i * b == num:
        res_list.append(i + b -2)

print(min(res_list))

Solution (Python):
c=int(input())
count=1
ans=10000000000000
k=c
for i in range(1,int(c**(1/2)+100000)):
    if c%i==0:
        if ans>i+int(c/i):
            ans=i+int(c/i)
        else:
            continue
print(ans-2)

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

Solution (Python):
import math 

N = int(input())
lst =[]


length= math.ceil(math.sqrt(N))
for i in range(length):
    if(N %(i+1) == 0):
        lst.append(i+1)
a = max(lst)
b = round(N/a)
ans = a+b -2
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
res = float('inf')
for i in range(1,10**6+1):
    if n%i == 0:
        res = min(res, i + n/i)
print(int(res-2))

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

ans = 0
for i in range(1,n):
    if n % i == 0:
      if ans == 0:
        ans = i + n//i
      else:
        ans = min(ans, i + n//i)
    if i**2 >= n:
      break

print(ans -2)

Solution (Python):
from math import sqrt

N = int(input())

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

print(ans)

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

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

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

a = int(N**0.5)
b = a+1

res = []
for i in range(1,a+1):
  if N%i == 0:
    res.append([i, int(N/i)])
    
ans = 10**12 + 10
for i in range(len(res)):
  ans = min(ans, sum(res[i]))
  
print(ans-2)

Solution (Python):
import math


N = int(input())

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

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

ma = int(N**0.5)

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

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

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

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

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

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


def solve(n):
    i = 1
    answer = n
    while i ** 2 <= n:
        q, r = divmod(n, i)
        if r == 0:
            answer = min(answer, i + q - 2)
        i += 1

    return answer


print(solve(n))

Solution (Python):
import math

def test():
    n = int(input())
    ans = float("inf")
    for i in range(1,int(math.sqrt(n)+1)):
        if n % i == 0:
            ans = min(i+(n//i)-2,ans)
    print(ans)


if __name__ == "__main__":
    test()

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

Solution (Python):

import sys
import heapq
import math

INF = float("inf")


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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=10**18
for i in range(1,int(n**.5)+2):
    if n%i<1:
        a=min(a,n//i+i-2)
print(a)

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

result = n

tmp = make_divisors(n)

for i in range(-(-len(tmp) // 2)):

   num0 = tmp[i]
   num1 = n // num0

   result = min(result, num0 + num1 - 2)

print(str(result))

Solution (Python):
import math


def solve():
    n = read()
    result = think(n)
    write(result)


def read():
    return read_int(1)[0]


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n):
    """
    >>> think(10)
    5
    >>> think(50)
    13
    >>> think(10000000019)
    10000000018
    """

    if is_prime(n):
        return n - 1  
    ceil = int(math.ceil(math.sqrt(n)))
    for a in range(ceil, -1, -1):
        if n % a == 0:
            b = n // a
            return (a - 1) + (b - 1)  


def write(result):
    print(result)


def is_prime(n):
    """
    >>> is_prime(0)
    False
    >>> is_prime(1)
    False
    >>> is_prime(2)
    True
    >>> is_prime(3)
    True
    >>> is_prime(4)
    False
    >>> is_prime(5)
    True
    >>> is_prime(6)
    False
    >>> is_prime(7)
    True
    >>> is_prime(8)
    False
    >>> is_prime(9)
    False
    >>> is_prime(10)
    False
    >>> is_prime(11)
    True
    >>> is_prime(12)
    False
    >>> is_prime(13)
    True
    >>> is_prime(14)
    False
    >>> is_prime(15)
    False
    >>> is_prime(16)
    False
    >>> is_prime(17)
    True
    >>> is_prime(18)
    False
    >>> is_prime(19)
    True
    >>> is_prime(20)
    False
    >>> is_prime(23)
    True
    >>> is_prime(25)
    False
    >>> is_prime(29)
    True
    >>> is_prime(30)
    False
    >>> is_prime(31)
    True
    """

    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False

    ceil = int(math.ceil(math.sqrt(n))) + 1
    for x in range(3, ceil, 2):
        if n == x:
            return True
        else:
            if n % x == 0:
                return False
    return True


if __name__ == '__main__':
    
    
    solve()

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

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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