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)

    divisors.sort()
    return divisors

divs = make_divisors(n)
lengths = list()
o = int(len(divs)//2 + len(divs) % 2)
for i in range(o):
    a = divs[i]
    b = divs[::-1][i]

    lengths.append(a-1+b-1)


print(min(lengths))

Solution (Python):
from itertools import product


def main():
    nn = n = input()
    if n == 1:
        print(0)
        return
    factors = {}
    p = 2
    while n % p == 0:
        factors[p] = factors.get(p, 0) + 1
        n //= p
    p = 3
    while nn >= p * p:
        while n % p == 0:
            factors[p] = factors.get(p, 0) + 1
            n //= p
        p += 2
    if n > 1:
        factors[n] = factors.get(n, 0) + 1
    
    s = []
    for f in factors:
        n = factors[f]
        s.append([(f ** i, f ** (n - i)) for i in xrange(n + 1)])

    best = float('inf')
    for case in product(*s):
        a = b = 1
        for aa, bb in case:
            a *= aa
            b *= bb
        best = min(best, a + b - 2)
    print(best)


main()

Solution (Python):
import numpy
import math

N = int(input())
k = math.floor(numpy.sqrt(N))
for i in range(k):
    if N%(k-i) == 0:
        print(int(k-i+N/(k-i)-2))
        break

Solution (Python):
n = int(input())
a = n - 1
p = 1
for c in range(2, int((n ** 0.5) * 2)):
    if p % 2 == 1 and c > (n ** 0.5):
        break
    if n % c == 0:
        p = c
        w = int(n / c)
        if (w + c - 2) < a:
            a = (w + c -2)
        else:
            continue
    else:
        continue
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)

    
    return divisors
N = int(input())
n_list = make_divisors(N)
move_list = list()
for i in n_list:
    a = N / i
    move_list.append(a+i-2)
move_list.sort()
print(int(move_list[0]))

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

def make_yakusu(x):
  all_yakusu=[]
  for i in range(1,int(x**0.5)+1):
    if x % i == 0:
      kumi_yakusu=[int(i), int(x / i)]
      all_yakusu.append(kumi_yakusu)
  return(all_yakusu)

tp = make_yakusu(N)
idouryou_list = []
for i in range(len(tp)):
  xn = tp[i][0]
  yn = tp[i][1]
  idouryou_list.append(int(xn) + int(yn) -2)
print(min(idouryou_list))

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

l = sorted(divisor(N))
kazu = math.ceil(len(l)/2)

now=0
for i in l[:kazu]:
    tmp = N/i
    tmp2 = i + tmp
    if now==0:
        now=tmp2
    if tmp2 < now:
        now=tmp2

print(int(now-2))

Solution (Python):
n=int(input())
s=n**0.5
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)
d.sort()
import bisect as bi
b=bi.bisect_left(d,s)
print(min(n//d[b-1]+d[b-1]-2,n//d[b]+d[b]-2))

Solution (Python):
import math

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

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 x.is_integer():
  a=len(make_divisors(N))
  b=sorted((make_divisors(N)))[a//2]
  print(2*b-2)
  
else:  
  c=len(make_divisors(N))
  d=sorted((make_divisors(N)))[c//2]
  e=sorted((make_divisors(N)))[(c//2)-1]
  print(d+e-2)

Solution (Python):


a = int(input())

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

print(min([x for x in make_divisors(a)]))

Solution (Python):
import math
n=int(input())
sn=int(math.sqrt(n))
tl=list(range(1,sn+1))
ans=0
for i in range(1,sn+1):
    a=n/tl[-i]
    if a.is_integer():
        ans=int(a)
        break
b=int(n/ans)
print(ans-1+b-1)

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

ans_i=0
ans_j=0
if len(a)==0:
    print(n-1)

else:
    min=abs(a[0]-b[0])
    for i in range(len(a)):
        if min >= abs(a[i]-b[i]):
            min=abs(a[i]-b[i])
            ans_i=a[i]
            ans_j=b[i] 
    print((ans_j-1)+(ans_i-1))

Solution (Python):
import numpy as np

A = int(input())

tmp = int(np.floor(np.sqrt(A)))
for i in range(tmp):
    if A % (tmp - i) == 0:
        base1 = tmp - i
        break
base2 = A // base1

print(base1 + base2 - 2)

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

i = 1
while i <= 10**6:
  if N % i == 0:
    list.append(int(i-1+N/i-1))
    i+=1
  else:
    i+=1
    
print(min(list))

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

first = int(np.sqrt(N))

for i in range(first, 0, -1):
    q, mod = divmod(N, i)
    if mod == 0:
        result = (i - 1) + (q - 1)
        break

print(result)

Solution (Python):
N = int(input())
min = N
max_i = 0
max_j = 0
for i in range(1,1000000):
    if N % i == 0:
        j = N // i
        if abs(i - j) < min:
            min = abs(i - j)
            max_i = i
            max_j = j
for i in range(1000000,N // 1000000 + 1):
    if N % i == 0:
        j = N // i
        if abs(i - j) < min:
            min = abs(i - j)
            max_i = i
            max_j = j

print(max_i + max_j - 2)

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

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

    
    return divisors

min_cnt = 10**13
divisor = make_divisors(N)

for i in range(len(divisor)):
    b = N // divisor[i]
    min_cnt = min(min_cnt, (divisor[i]-1 + b-1))
print(min_cnt)

Solution (Python):
i = 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 = []
for x, y in zip(sorted(make_divisors(i)), sorted(make_divisors(i),reverse=True)):
	l.append(x + y - 2)
print(min(l))

Solution (Python):
import math

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

if N == 2 or N == 3:
    print(N-1)
elif sq*sq == N:
    print(2*sq-2)
else:
    for i in reversed(range(1,sq+1)):
        if N % i == 0:
            print(i+(N//i)-2)
            break

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):
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 sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
N = I()

ans = int(0)
cou = []

cou = make_divisors(N)
if cou[-1]!=int(N**(0.5)):
    ans = cou[-1]+cou[-2]-2
else:
    ans = 2*cou[-1]-2


print(ans)

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)

result = sorted(divisors)


if len(result) == 2:
    final_i = result[0]
    final_j = result[1]
else:
    
    if len(result)%2 == 0:
        final_i = result[len(result)//2 - 1]
        final_j = result[len(result)//2 ]
            
    else:
        final_i = result[(len(result))//2]
        final_j = result[(len(result))//2]
            

print(final_i+final_j - 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)

    divisors.sort()
    return divisors

n = int(input())

arr = make_divisors(n)

useIndex = int(len(arr)/2)


x = arr[useIndex - 1] - 1
y = arr[useIndex] - 1

print(x + y)

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())
ret = make_divisors(N)
if ret[-1]**2 == N:
    ret.append(ret[-1])

min = 9999999999999
for i in range(int(len(ret)/2)):
    now = (ret[i*2] -1) + (ret[i*2+1] -1)
    min = now if now < min else min
    
print(min)

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

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


l = len(divisors)
if l % 2 == 0:
    a = divisors[:int(l/2)]
    b = divisors[int(l/2):]
    b.sort(reverse=True)
    c = []
    for i in range(int(l/2)):
        c.append(a[i]+b[i])
    print(min(c)-2)
        
else:
    a = divisors[:int(l/2)]
    b = divisors[int((l+1)/2):]
    b.sort(reverse=True)
    c = divisors[int((l-1)/2)]
    d = []
    for i in range(int((l-1)/2)):
        d.append(a[i]+b[i])
    print(min(min(d), c*2) - 2)

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

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

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

    
    return divisors

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

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

Solution (Python):
N=int(input())
import math
SQ=int(math.sqrt(N))
minval=10**12
a=1
b=1
for i in range(1,SQ+1):
  if N%i==0:
    newa=i
    newb=N//i
    if abs(newa-newb) < minval:
      minval=abs(newa-newb)
      a=newa
      b=newb

print(a+b-2)

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

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

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

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

Solution (Python):
N = int(input())
import math
def prime(n):
  sq = math.ceil(n**(1/2))
  for i in range(sq,1,-1):
    if n%i==0:
      return i,False
    
  return 1,True

A,B = prime(N)
if B:
  print(N-1)
  
else:
  print((N//A+A-2))

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

    
    return divisors

N = int(input())

A = make_divisors(N)
result = pow(10,12)

for i in A:
    
    B = (N/i-1)+(i-1)
    
    result = min(result,B)
    
print(int(result))

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

cd = []
for i in range(1, int(n**0.5)+1):
    if n % i == 0:
        cd.append(i)
        if i != n // i:
            cd.append(n//i)
min = float('inf')
if len(cd) % 2 == 0:
    for i in range(0, len(cd) - 1,  2):
        if cd[i] + cd[i+1] < min:
            min = cd[i] + cd[i+1]
    print(min - 2)
else:
    print(cd[len(cd) - 1] * 2 - 2)

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

print(m)

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

def balance_fact(n):
  rn = int(math.sqrt(n))
  m = 1
  k = 0
  for i in range(rn):
    j = rn-i
    if n%j == 0:
      m = n//j
      k = j
      break
  return (m, k)  
    
m, k = balance_fact(n)
print(m+k-2)

Solution (Python):
import math
n=int(input())
def is_prime(n):
    if n == 1: return False

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

    return True
if is_prime(n)==True:
    print(int(n-1))
else:
    N=int(math.sqrt(n))
    for i in reversed(range(N)):
        if n%(i+1)==0:
            print(int((n/(i+1))+i-1))
            break

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

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

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

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

a = int(input())
b = make_divisors(a)
idx = len(b) // 2
if len(b) % 2 == 0:
  target = (b[idx],b[idx-1])
else:
  target = (b[idx],b[idx])
print(target[0]-1 + target[1]-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)

    divisors.sort()
    return divisors

N = int(input())
list =make_divisors(N)
list_rev=sorted(list, reverse=True)

ans = 10**15
for i in range(len(list)//2 + 1):
  tmp = (list[i]-1)+(list_rev[i]-1)
  if tmp < ans:
    ans=tmp

print(ans)

Solution (Python):
from math import sqrt
from math import ceil

min_move = 10**12

n = int(input())

for i in range(1,ceil(sqrt(n)+0.1)):
  if n%i == 0:
    
    b = n//i
    if min_move >  i+b-2:
      min_move = i+b-2

print(min_move)

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

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()
ans = []
j_list = []

for j in divisors:
  if int(N/j) in j_list:
    break
  ans.append(j+int(N/j)-2)
  j_list.append(j)

print(min(ans))

Solution (Python):
abc = input()
abc_int = int(abc)
prolist = []
chk = int(abc_int**0.5)+2
for c1 in range(chk):
	if abc_int % (c1+1) == 0:
		prolist.append(c1+1+abc_int//(c1+1)-2)
print(min(prolist))

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

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

print(ans)

Solution (Python):
import math
import itertools


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())
d = make_divisors(n)
len = len(d)
if len % 2 == 0:
    print((d[int(len/2)]-1)+(d[int(len/2-1)]-1))
else:
    print((d[int((len-1)/2)]-1)+(d[int((len-1)/2)]-1))

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

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

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

Solution (Python):
import math
import bisect

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

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(x)
li.sort()
alpha = bisect.bisect_left(li,n,hi = len(li))

if int(n) ** 2 == x:
  print(2 * int(n) - 2)
else:
  print(li[alpha -1] + li[alpha] - 2)

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

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

divisors = make_divisors(N)
length = len(divisors)
if length % 2 == 1:
    ans = (divisors[length // 2] * 2) - 2
    print(ans)
    exit()

if length % 2 == 0:
    ans = divisors[(length // 2)-1] + divisors[length // 2] - 2
    print(ans)
    exit()

Solution (Python):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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


n=one_int()

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














divisors=sorted(divisors,key=lambda x:x[2])[0]

print(divisors[2])

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

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

    
    return divisors



n = int(input())
a=make_divisors(n)
if len(a)%2==0:
    print(a[-1]+a[-2]-2)
else:
    print(a[-1]*2-2)

Solution (Python):
x = int(input())
cand_list = []

for i in range(1, int(x**(1/2))+1):
    if x%i==0:
      cand_list.append([i, x//i])

ans = min([sum(cand_i)-2 for cand_i in cand_list])
if x == 2:
  ans = 1
elif x == 3:
  ans = 2

print(ans)

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

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

d=divisors(N)
l=len(d)
if l%2==0:
  a=d[int(l/2)]+d[int(l/2-1)]
else:
  a=d[int((l-1)/2)]*2
print(a-2)

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

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

    divisors.sort()
    return divisors

n=int(input())
l = make_divisors(n)
if len(l)%2==0:
    print((l[len(l)//2-1]-1)+(l[len(l)//2]-1))
else:
    print((l[len(l)//2]-1)+(l[len(l)//2]-1))

Solution (Python):

import numpy as np


N = int(input())

nn = np.sqrt(N)
ni = int(nn)

lis = []
for i in range(1, ni+1):
  if(N % i == 0):
    lis.append(i)

ii = lis[-1]
jj = int(N / ii)
step = (ii-1) + (jj-1)

print("{}".format(step))

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

Solution (Python):
n=int(input())
mini = (10** 12)+1
X=0
Y=0
t=0
K=0
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 = make_divisors(n)
ans.sort()
K+=len(ans)
if K%2:
  for i in range(int((K+1)/2)):
    t=-(i+1)
    if mini>ans[t]-ans[i] and n==ans[t]*ans[i] and ans[t]>=ans[i]:
      mini =ans[t]-ans[i]
      X=ans[t]
      Y=ans[i]
else:
  for i in range(int(K/2)):
    t=-(i+1)
    if mini>ans[t]-ans[i] and n==ans[t]*ans[i] and ans[t]>=ans[i]:
      mini =ans[t]-ans[i]
      X=ans[t]
      Y=ans[i]
print(X-1+Y-1)

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

Solution (Python):
def yakusu(n):
        result = []
        for i in range(1,int(n**0.5)+1):
                if n % i == 0:
                        result.append(i)
                        if i != n // i:
                                result.append(n//i)
        result.sort()
        return result
yakusu = yakusu(int(input()))
m = len(yakusu)
if m % 2 ==0:
        print(yakusu[m//2-1]+yakusu[m//2+1-1]-2)
else:
        print((yakusu[m//2]-1)*2)

Solution (Python):
import math

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

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

Solution (Python):
import math
a=int(input())
kyori=a
dest = int(math.sqrt(a))
for i in range(2,dest+1):
  if a % i == 0:
    if i+(a//i)-2 < kyori:
      kyori=i+(a//i)-2
if kyori==a:
  print(a-1)
else:
  print(kyori)

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)
l.sort()
if len(l)%2 == 0:
    a = l[(len(l)//2)-1]-1
    b = l[(len(l)//2)]-1
    print(a+b)
else:
    a = l[(len(l)//2)]-1
    print(a*2)

Solution (Python):
import math

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

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

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


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


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    A, B = LI()

    if A >= 1 and A <= 9 and B >= 1 and B <= 9:
        print(A*B)
    else:
        print(-1)
    return




def B():
    N = II()
    if N <= 81 and N > 9:
        for i in range(2, 10):
            if N % i == 0 and N/i <= 9:
                print('Yes')
                return

    elif N > 81:
        print('No')
        return
    else:
        print('Yes')
        return
    print('No')

    return




def C():
    N = II()
    
    
    L=[]
    count=0
    for i in range(2,int(sqrt(N)+2)):  
        if N%i==0:
            L.append(N/i)
            count=1
    if count==0:
        print(N-1)
    else :
        print(int(L[-1]-2+(N/L[-1])))


            

    
   

    return




def D():
    N = II()
    L = LI()
    L.sort()

    count = 0
    for i in range(N-2):

        for j in range(i+1, N-1):

            p = L[i]+L[j]

            count += sum(x < p for x in L)-j-1

    print(count)

    return




def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return




def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    



def solution(S):
    
    N = len(S)

    pass



if __name__ == '__main__':
    C()

Solution (Python):
n = int(input())
x, y = 1,1

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

s = make_divisors(n)
l  = []
res = 10e+12
for i in range(len(s)):
    a = s[i] - x+  n // s[i] - y 
    if res > a:
        res = a
print(res)

Solution (Python):
k = 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(k)
a.sort()
mini = 10000000000000
for i in range (len(a)):
	ii = a[i]
	jj = k / a[i]
	wa = ii - 1 + jj - 1
	mini = min(wa, mini)
	if a[i] > k//2:
		break
print (int(mini))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def prime_decomposition(n): 
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(int(n))
  return table

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

k = make_divisors(n)
k.sort()

ans = Max

for i in range(len(k)):
    if ans >= n//k[i] + k[i] - 2:
        ans = n//k[i] + k[i] - 2
        
print(ans)

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

Solution (Python):
import math

import sys
import math

def Sieve_of_Eratosthenes(N):
    if N<2: return set()
    prime=set(i for i in range(2,N+1))
    for i in range(2,math.ceil(N**0.5)+1):
        tmp=set()
        for j in prime:
            if j>i and j%i==0: tmp.add(j)
        for j in tmp:
            prime.remove(j)

    return list(prime)

N=int(input())


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

print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest
import math


def resolve():
    N = int(input())
    A = 1
    B = 1

    center = math.ceil(math.sqrt(N))
    for i in range(1, center + 1)[::-1]:
        if N % i == 0:
            A = i
            B = int(N / i)
            break
        else:
            continue

    result = A - 1 + B - 1
    print(result)


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):
n = int(input())
lim = int(n**0.5)+1
while(lim != 1):
    if n%lim == 0:
        break
    lim -= 1
print(lim+n//lim-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


divisors = make_divisors(int(input()))
sub_list = []
for i in range(1, len(divisors), 2):
    sub_list.append(divisors[i] - divisors[i-1])

min_index = sub_list.index(min(sub_list))
print(divisors[min_index * 2] + divisors[(min_index+1) * 2 - 1] - 2)

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

res = 10**20
for i in range(1,10**6+1):
    if n%i == 0:
        res = min(res,i+n//i)
print(res-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
ans = N - 1
lst = make_divisors(N)
if (len(lst) % 2 == 0):
    for i in range(0, len(lst), 2):
        num = lst[i] + lst[i+1] - 2
        ans = min(ans,num)
else:
    for i in range(0, len(lst)-1, 2):
        num = lst[i] + lst[i+1] - 2
        ans = min(ans,num)
    ans = min(ans,2*lst[-1] - 2)
print(ans)

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

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

for n in reversed( range(1, root_N+1) ):
    if N%n == 0:
        n_x = n
        n_y = int(N/n)
        break
print(n_x+n_y-2)