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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def isqrt(n):
    if n > 0:
        x = 1 << (n.bit_length() + 1) // 2
        y = (x + n // x) // 2
        while y < x:
            x = y
            y = (x + n // x) // 2
        return x
    elif n == 0:
        return 0
    else:
        raise ValueError("isqrt() argument must be nonnegative")


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

    n = isqrt(N)
    ans = INF
    for i in range(1, n + 1):
        if N % i == 0 and ans > N // i + i - 2:
            ans = N // i + i - 2

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import math
def main():
    N = int(input())
    min_sum_xy = N + 1
    for x in range(2, math.floor(N**(1/2))+1):
        if N % x == 0:
            y = N // x
            min_sum_xy = min(x + y, min_sum_xy)
    print(min_sum_xy - 2)
main()

Solution (Python):
import math
def main():
    N = int(input())
    min_sum_xy = N + 1
    for x in range(2, math.floor(N**(1/2))+1):
        if N % x == 0:
            y = N // x
            min_sum_xy = min(x + y, min_sum_xy)
    print(min_sum_xy - 2)
main()

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

a=1
b=N//a
dis=abs(b-a)

for i in range(1,int(N**0.5)+1):
    if N%i==0:
        _a=i
        _b=N//i
        _dis=abs(_b-_a)
        if _dis<dis:
            a=_a
            b=_b
            dis=_dis

print(a-1+b-1)

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

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

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

m= int(n**0.5)

while m >= 1:
    if n%m ==0:
        print(int(n/m)+m-2)
        exit(0)

    m -=1

Solution (Python):
n = int(input())
def divisor(n): 
    i = 1
    out = float('inf')
    while i * i <= n:
        if n%i == 0:
          out = min((i+n//i),out)
        i += 1
    return out
print(divisor(n)-2)

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

m= int(n**0.5)

while m >= 1:
    if n%m ==0:
        print(int(n/m)+m-2)
        exit(0)
    else:
        m -=1

Solution (Python):
n = int(input())
def divisor(n): 
    i = 1
    a = 0
    b = 0
    out = float('inf')
    while i * i <= n:
        if n%i == 0:
            a = i
            b = n//i
        if a+b < out:
          out = a+b
        i += 1
    return out
print(divisor(n)-2)

Solution (Python):
def main():
    n = int(input())
    i = 2
    minv = int(10**12)
    while i*i <= n:
        if n % i == 0:
            a = i
            b = int(n / i)
            minv = min(minv, a+b-2)
        i += 1
    if minv < int(10**12):
        print(minv)
    else:
        print(n-1)

if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
s=(n**(1/2))//1
t=0
while t==0:
    if n%s==0:
        print(int(s+(n//s)-2))
        t+=1
    else:
        s=s-1

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

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

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
  
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
if is_prime(n):
  print(n-1)
  exit()
  

div=make_divisors(n)
l=[]
for i in div:
  ii=n//i
  l.append(ii+i)

print(min(l)-2)

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

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

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

a = math.ceil(math.sqrt(N))

yaku = {1,N}

for i in range(2,a+1):
  if N % i == 0:
    yaku.add(i)
    yaku.add(N // i)

sl = len(yaku)
lyaku =list(yaku)
lyaku.sort()
if sl % 2 == 1:
  print((lyaku[sl//2]-1)*2)
else:
  print(lyaku[sl//2]+lyaku[sl//2-1] -2)

Solution (Python):
import math
N = int(input())
MIN = math.inf
Limit = math.ceil(math.sqrt(N))
for T in range(1,Limit+1):
    if N%T==0 and T+N//T<=MIN:
        MIN = T+N//T
print(MIN-2)

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

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

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

resolve()

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

minimum = n
for i in range(1, n2 + 1): 
    if (n % i == 0):    
        minimum = min(minimum, (n // i -1) + (i - 1))


print(minimum)

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

n = len(divisors)
i = len(divisors) // 2

A, B = 0, 0
if n % 2 == 0:
    A, B = divisors[i - 1], divisors[i]
else:
    A, B = divisors[i], divisors[i]

print(A - 1 + B - 1)

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

while n%r!=0:
  r-=1
print(n//r+r-2)

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

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

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

Solution (Python):


import math
import sys
N = int(input())

def divisor(N):
    lst = []
    for i in range(2,math.ceil(math.sqrt(N))+1):
        if N%i == 0:
            lst.append(i)
            
                
    
    return lst
    
ans = divisor(N)

if N == 2:
    print(1)
    sys.exit()
    
if len(ans) > 0:
    A = ans[-1]
    if A*A < N:
        B = int(N/A)
        print(A+B-2)
    else:
        print(2*A-2)
else:
    print(N-1)

'''
if len(ans)%2 == 0 and len(ans)>1:
    A = ans[len(ans)//2-1]
    B = ans[len(ans)//2]
    
    print(A+B-2)
elif len(ans)%2 == 1 and len(ans)>1:
    A = ans[len(ans)//2]
    print(2*A-2)
else:
    print(N-1)
'''

Solution (Python):
import math

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

minimum=n
for i in range(1,n2+1):
    if(n%i==0):
        minimum=min(minimum,(n//i-1)+(i-1))

print(minimum)

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

N_sq=N**(1/2)

for i in range(1,int(N_sq)+1):
    if N%i ==0:
        div = [i,int(N/i)]
        
print(sum(div)-2)

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

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

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

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

Solution (Python):
n,a=int(input()),float("inf")
for i in range(1,5*10**6+1):
	if n%i==0:j=n//i;a=min(a,i+j-2)
print(a)

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

Solution (Python):
N=int(input())
move=int(N**.5)
while N%move!=0:
    move-=1
print(N//move+move-2)

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

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

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

N = int(input())
L = divisor(N)
L = sorted(L)
ans = float("inf")

for i in L:
    p = i
    q = N // i
    temp = (p-1)+(q-1)
    ans = min(ans, temp)

print(ans)

Solution (Python):
n=int(input())
r=100000000000000000000000000000
for i in range(1,int(pow(n,0.5))+1):
  if(n%i==0):
    r=min(r,abs(i-1)+abs(n//i-1))
print(r)

Solution (Python):
import math

N = int(input())

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

lst = yakusuu(N)

ans = 10**18
for i in range(len(lst)):
    a = lst[i]
    b = N//lst[i]
    
    ans = min(ans, (a-1)+(b-1))
print(ans)

Solution (Python):
import math

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

for i in range(1, int(math.sqrt(n))+1):
    j = n // i
    if n == i*j:
        tmp_ans = (i-a) + (j-b)
        ans = min(ans, tmp_ans)
print(ans)

Solution (Python):
import math

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

for i in range(1, int(math.sqrt(n))+1):
    j = n // i
    if n == i*j:
        tmp_ans = (i-a) + (j-b)
        if tmp_ans < ans:
            ans = tmp_ans
print(ans)

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

n = int(input())
a = 1
b = 1
tmp_ans = 0
ans = 10**12

if is_prime(n) == True:
    print(n - 1)
elif is_prime(n) == False:
    for i in range(2, int(math.sqrt(n))+1):
        j = n // i
        if n == i*j:
            tmp_ans = (i-a) + (j-b)
            if tmp_ans < ans:
                ans = tmp_ans
    print(ans)

Solution (Python):
import sys

def I(): return int(sys.stdin.readline().rstrip())

N = I()

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



d.sort()
a = len(d)

print(d[a//2] + d[a//2-1] - 2)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

N = I()

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

d.sort()
a = len(d)

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

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

Solution (Python):






import math





N = int(input())





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

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

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

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

Solution (Python):
N=int(input())
move=int(N**.5)
while N%move!=0:
    move-=1
print(N//move+move-2)

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

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

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

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

print(i + j -2)

Solution (Python):
import math

n = int(input())

t = int(math.sqrt(n))

ans = 0

for i in reversed(range(t+1)):
    if n % i == 0:
        ans = n//i+i
        break
        
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)

    divisors.sort()
    return divisors

n = int(input())
a = (max(make_divisors(n)))
b = n//a
print(a+b-2)

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

def main():
    n = int(input())
    min_ = n - 1
    for a in range(2, int(math.sqrt(n))+1):
        if n % a == 0:
            b = n // a
            min_ = min((a-1) + (b-1), min_)
    print(min_)
    
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
def makediv(n):
	lower_divisors , upper_divisors = [], []
	i = 1
	while i*i <= n:
		if n % i == 0:
			lower_divisors.append(i)
			if i != n // i:
				upper_divisors.append(n//i)
		i += 1
	return lower_divisors + upper_divisors[::-1]
k = makediv(n)
ans = 10**18
for i in k:
	t = n//i
	ans = min((t-1)+(i-1), ans)
print(ans)

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

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

Solution (Python):
import os, sys, re, math

N = int(input())

s = math.sqrt(N)
upper = math.ceil(s)
lower = math.floor(s)

val1 = N
val2 = N
while val1 == N and val2 == N:
    if N % upper == 0:
        val1 = N // upper + upper - 2
    if N % lower == 0:
        val2 = N // lower + lower - 2
    upper += 1
    lower -= 1

print(min(val1, val2))

Solution (Python):
import math

n = int(input())
nn = int(math.sqrt(n))
for i in range(nn, 0, -1):
	if n / i == n // i:
		j = n // i
		print (i - 1 + j - 1)
		exit()

Solution (Python):


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

print(result)

Solution (Python):
n = int(input())
ans = 100000000000000
for i in range(1, 1000001):
  if n % i == 0:
    a = i - 1
    b = n // i - 1
    ans = min(ans, a + b)
print(ans)

Solution (Python):
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
b=int(math.sqrt(a))
for i in range(b):
  if a%(b-i)==0:
    print(int(a/(b-i))+b-i-2)
    sys.exit()

Solution (Python):

from math import sqrt

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

Solution (Python):
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

N=int(input())
div=make_divisors(N)

from math import sqrt

tmpmin=10**16
for i in range(len(div)):
  if tmpmin>abs(sqrt(N)-div[i]):
    tmpmin=abs(sqrt(N)-div[i])
    minidx=i

a=div[minidx]
b=N//a

print(a-1+b-1)

Solution (Python):
n = int(input())
lower_divisors, upper_divisors = [], []
i = 1
while i*i <= n:
    if n % i == 0:
        lower_divisors.append(i)
        upper_divisors.append(n // i)
    i += 1

ans = (lower_divisors[-1] - 1) + (upper_divisors[-1] - 1)

print(ans)

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

print(ans)

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

Solution (Python):
N = int(input())
mi = 10**15
ans_i = 0
ans_j = 0
for i in range(2, 10**6+1):
    if N % i == 0:
        j = N // i
        if mi > (i+j-2):
            mi = i+j-2
            ans_i = i
            ans_j = j
if ans_i+ans_j != 0:print(ans_i+ans_j-2)
else:print(N-1)

Solution (Python):
n=int(input())
tmp=float("inf")
for a in range(1,int(n**(1/2))+1):
    if n%a==0:
        b=n/a
        if tmp>a+b:
            tmp=a+b
print(int(tmp-2))

Solution (Python):
N=int(input())
move=10**12
for n in range(1,int(N**.5)+1):
    if N%n==0:
        move=min(move,N//n+n-2)
print(move)

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

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

ans = 10**13

for a in range(1,math.ceil(math.sqrt(n))+1):
    b = n//a
    if a*b == n:
        ans = min(a+b-2,ans)
    else:
        pass

print(ans)

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


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

print(min(ans))

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

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

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

Solution (Python):
from math import ceil
n=int(input())
x=1
for i in range(ceil(pow(n,0.5)),1,-1):
  if n%i==0:
    x=i
    break
y=n//x
print(x+y-2)

Solution (Python):
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors

n=int(input())

l=make_divisors(n)

print(n//l[-1]+l[-1]-2)

Solution (Python):
N=int(input())
move=10**12
for n in range(1,int(N**.5)+1):
    if N%n==0:
        move=min(move,N//n+n-2)
print(move)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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


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

    div = make_divisors(n)
    res = f_inf
    for num in div:
        x, y = num, n // num
        res = min(res, x - 1 + y - 1)
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):



n = int(input())

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

print(ans)

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

ans = N - 1
for i in range(2, N + 1):
    if i * i > N:
        break
    div, mod = divmod(N, i)
    if mod == 0:
        v = i - 1 + div - 1
        ans = min(v, ans)
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

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

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

N = int(input())
ans = 10**18
i = 1

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

print(ans)

Solution (Python):
import math

n = int(input())

v = math.floor(math.sqrt(n))
while True:
    if n % v == 0:
        print(v + (n // v) - 2)
        exit()
    v -= 1

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

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

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

Solution (Python):
import math

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

for i in reversed(range(1,n+1)) :
    if N % i == 0 :
        a = i
        b = N // i
        break

print(a+b-2)

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

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

x=1
y=1
for i in range(n_sqrt):
    if n%(i+1)==0:
        x=i+1
        y=n//(i+1)

print(x+y-2)

Solution (Python):
import math
import numpy as np

n = int(input())
n_2 = int(math.sqrt(n) + 1)
x = np.arange(1, n_2)
div = x[n % x == 0]
ans = 10 ** 12
for i in div:
    j = (n // i) + i - 2
    ans = min(ans, j)
print(ans)

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

Solution (Python):
import numpy as np

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

print(ans)

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

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

print(ans)

Solution (Python):
import sys
from itertools import permutations

N = int(input())
i, j = 1, 1
max0 = -1
ans = 0
while i <= N // i:
    if N % i != 0:
        i += 1
        continue
    else:
        j = N // i
        if max0 > abs(i-j) or max0 == -1:
            max0 = abs(i-j)
            ans = i + j
        i += 1
print(ans-2)

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

print(least-2)

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

import math
N = int(readline())

ans = N

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


print(ans)

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

i = 1
while True:
   if (i ** 2) >  N:
      break
   else:
      i += 1
while i > 1:
   if N % i == 0:
      break
   else:
      i -= 1
j = N // i
print(i + j - 2)

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

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

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

solve()

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction




def main():
    num = int(input())
    ans = num - 1

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

    print(ans)




if __name__ == '__main__':
    main()

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

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

for i in range(int(math.sqrt(N))):
    j=N/(i+1)
    if j%1==0:
        if i+int(j)-1<ans:
            ans=i+int(j)-1

print(ans)

Solution (Python):

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

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

for i in range(1,int(math.sqrt(a))+1):
    if a % i == 0:
        sum.append((a/i)+i)


print(int(min(sum)-2))

Solution (Python):
import math

N = int(input())
ans = float("inf")



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

print(ans)

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


flag = 0
for i in range(2, int(math.sqrt(N))+1, 1):
  if N % i == 0:
    flag = 1
    break
if flag == 0:
  print(N-1)
else:
  res = 0
  idx = int(math.sqrt(N))
  for i in range(idx, 0, -1):
    if N % i == 0:
      res = i + int(N/i) - 2
      print(res)
      break

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

Solution (Python):
import math
N=int(input())
sqn=math.floor(math.sqrt(N))

for fac in reversed(range(sqn+1)):
    if N%fac==0:
        print(int(fac+N/fac-2))
        break

Solution (Python):
import numpy as np
N = int(input())
A = []
i = 2
a = int()
b = int()
ans = 10**12+1
for i in range(2,int(np.sqrt(N))+1):
    if N%i ==0:
        A.append(i)

if len(A) == 0:
    print(N-1)
else:
    for j in range(len(A)):
        a = A[j]-1
        b = (N/A[j])-1
        if a+b <= ans:
            ans = a+b
    print(int(ans))

Solution (Python):
import math

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


minimum=n-1
for i in range(1,n2+1):
    
    
    if(n%i==0):
        
        
        
        minimum=min(minimum,(n//i-1)+(i-1))

print(minimum)

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

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

ans = 10**12
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())

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

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

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

print(ans)

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

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

num_li = make_divisors(N)
n = len(num_li)

if n == 2: 
    print(num_li[1]-1)
elif n%2 == 1:
    print((num_li[(n-1)//2]-1)*2)
else:
    print((num_li[n//2]-1)+(num_li[n//2-1]-1))

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

Solution (Python):
import fractions

def make_divisor_list(num):
    i = 1
    table = []
    while i * i <= num:
        if num%i == 0:
            table.append(i)
            table.append(num//i)
        i += 1
    table = list(set(table))
    return table

N = int(input())

divisors = sorted(make_divisor_list(N))
center = int(len(divisors) / 2)

if len(divisors) % 2 == 1:
    ans = (divisors[center] - 1) * 2
else:
    ans = divisors[center] - 1 + divisors[center-1] - 1

print(ans)

Solution (Python):
import math
N = int(input())
N_s = int(math.sqrt(N))

ans_list = []
ans = 0

for i in range(N_s):
  if(N%(i+1) == 0):
    ans = N/(i+1) + (i+1) - 2
    ans_list.append(int(ans))
    
ans_list = sorted(ans_list)
print(ans_list[0])

Solution (Python):


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

if __name__ == '__main__':
    main()

Solution (Python):

"""
C - Walk on Multiplication Table
https://atcoder.jp/contests/abc144/tasks/abc144_c

"""
import sys


def divisor(n):
    divisors = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            divisors.add(n // i)
    return divisors


def solve(N):
    ans = float('inf')
    for d in divisor(N):
        q = N // d
        ans = min(ans, d-1 + q-1)
    return ans


def main(args):
    N = int(input())
    ans = solve(N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import math

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

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

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

end=0
while start>0:
    if N%start==0:
        end=N//start
        break
    else:
        start-=1

print(end-1+start-1)

Solution (Python):
import math

def main():
    n = int(input())
    root = []
    for i in range(1, int(math.sqrt(n))+1):
        if n % i == 0:
            root.append(i+(n//i)-2)
    print(min(root))
    
if __name__ == "__main__":
    main()

Solution (Python):
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

N = int(input())
l = make_divisors(N)


ans = float("inf")
if len(l)%2:
    ans = l[len(l)//2]*2-2
else:
    for i in range(len(l)//2):
        ans = min(ans, l[i]-1+N//l[i]-1)
print(ans)

Solution (Python):
import math

N = int(input())

A = math.floor(N**0.5)
B = math.ceil(N**0.5)

while True:
    if N % A == 0:
        B = N // A
        break
    elif N % B == 0:
        A = N // B
        break
    else:
        A -= 1
        B += 1

print(A + B - 2)

Solution (Python):

N = int(input())
res = float('inf')
for i in range(1,int(N**0.5)+1):
  if N%i==0:
    x,y = i-1,N//i-1
    res = min(res, x+y)

print(res)

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


def main():
    n = int(input())
    divs = sorted(divisor(n))
    if len(divs) % 2 == 0:
        left = len(divs)//2 - 1
        right = len(divs)//2
    else:
        left, right = len(divs)//2, len(divs)//2

    ans = divs[left] + divs[right] - 2
    print(ans)


if __name__ == "__main__":
    main()