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())
ans = 10**12
for i in range(1,int(N**0.5)+1):
    j = N/i
    if(j == int(j)):
        ans = i+j-2
print(int(ans))

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

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

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

furui = [i for i in range(10**6+2)]

ans = 9999999999999

yakusuu = []

for i in range(1,int(n**0.5)+1+1):
    
    if n%i == 0:
        
        yakusuu.append(i)
        

    
for i in yakusuu:
    
    ans = min(i+n//i,ans)
    
    
print(ans-2)

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

Solution (Python):
N = int(input())
N_ri = round(pow(N, 1/2))

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

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    n = int(input())
    ans=10**12
    tmp=[]
    for a in range(1,10**6+1):
        if n%a==0 and n >=a:
            b=n//a
        elif n <a:
            break
        
        ans=min(ans,a+b-2)
    print(ans)

if __name__=="__main__":
    main()

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    n = int(input())
    ans=10**12
    for a in range(1,10**6+1):
        if n%a==0:
            b=n//a
        ans=min(ans,a+b-2)
    print(ans)

if __name__=="__main__":
    main()

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

ans = float("inf")
for i in range(1,int(math.sqrt(n)+1)):
    if n%i == 0:
        if (i-1)+(n//i-1) < ans:
            ans = (i-1)+(n//i-1)

print(ans)

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

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

a,b = 1,n
for i in range(1,int(math.sqrt(n)+1)):
    if n%i == 0:
        q = n//i
        if abs(a-b) > abs(q-i):
            a,b = i,q

print((a-1) + (b-1))

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

N = int(input())
m = N

if len(divisor(N))==1:
  print(N-1)

else:
  for i,j in divisor(N):
    m = min(m,i+j)
  print(m-2)

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

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

num = set()
for n in range(2, 10**7):
    if N % n == 0:
        num.add(tuple(sorted([n, N//n])))
    if N == n:
        break
ans = (10**12, 10**12)
if not num:
    print(N - 1)
else:
    for s in num:
        if s[0]+s[1] < ans[0]+ans[1]:
            ans = s
    print(ans[0] + ans[1] - 2)

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

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

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

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

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

def main():
    N = int(input())
    d = make_divisors(N)
    d.reverse()
    ans = float('inf')
    for i in range(N):
        if len(d) == 1:
            a = d.pop()
            ans = min(ans, 2*(a-1))
            break
        if len(d) == 0:
            break
        a = d.pop()-1
        b = d.pop()-1
        ans = min(ans, a+b)
    print(ans)

if __name__ == "__main__":
    main()

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



cand = 1
n = 1
while n * n <= N:
  if N % n == 0:
    cand = n
  n += 1

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

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

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

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

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

result = [10**13]
cnt = []
for i in range(1,int(math.sqrt(N))+1) :
    if N % i == 0 :
        buf = min(result[-1],i+(N//i))
        result.append(buf)
        if result[-1] != result[-2] :
            cnt.append([i,N//i])


print(cnt[-1][0]-1+cnt[-1][1]-1)

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

print(int(ans))

Solution (Python):
import math

n = int(input())
List = []
n_sqrt = int(math.sqrt(n))

for i in range(1,n_sqrt+1):
    if n % i == 0:
        number = i + (n // i) -2
        List.append(number)
        
print(min(List))

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

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

Solution (Python):
import sys

n=int(input())

c=[]

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

if len(c)==0:
    print(n-1)
    sys.exit()
    
print(min(c))

Solution (Python):

from math import sqrt

def main():
    N = int(input())
    ans = 1e12 + 1
    for i in range(1, int(sqrt(N)) + 1):
        if N % i == 0:
            j = N // i
            ans = min(ans, i + j - 2)

    print(ans)

if __name__ == "__main__":
    main()

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

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

Solution (Python):
import math

N = int(input())
N_ = int(math.sqrt(N)) + 1

min_distance = N - 1
for i in range(1, N_):
    p, r = divmod(N, i)
    if r == 0:
        if 1 <= p <= N:
            distance = (i-1) + (p-1)
            min_distance = min(min_distance, distance)
        else:
            continue

print(min_distance)

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 resolve():
    n = int(input())
    ans = float('inf')
    c = divisor(n)
    for a in c:
        b = n//a
        ans = min(ans,a+b-2)
    print(ans)
resolve()

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

print(ans)

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

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

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

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())
m = 10**13
for x in range(1,1000001):
  y = n / x
  if y % 1 == 0:
    v = x + y -2
    m = min(m, v)
print(int(m))

Solution (Python):
from math import sqrt
n = int(input())
minSoFar = 10**12+1

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

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

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()
m=len(divisors)

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

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

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

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

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

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

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

def low_divisors(N):
  K=1
  low_divs = []
  while K*K <= N:
    if N%K == 0:
      low_divs.append(K)
    K += 1

  return low_divs

L_divs = low_divisors(N)
D = []

for i in range(len(L_divs)):
  D.append(L_divs[i]+N//L_divs[i])
  
print(min(D)-2)

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

ans = float("inf")
flag = 0
for i in range(2,int(n**(1/2))+1):
  if n%i == 0:
    ans = min(ans, i+(n//i)-2)
    flag = 1

if flag:
  print(ans)
else:
  print(n-1)

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



x = 1
mn = n-1
while x**2 <= n:
    if n % x == 0:
        y = n//x
        mn = min(mn, x+y-2)
    x+=1
    
print(mn)

Solution (Python):
def abc144c_walk_on_multiplication_table():
    def divisor(n):
        """ nの約数列挙 """
        ret = []
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                ret.append(i)
                if i ** 2 == n:
                    continue
                ret.append(n // i)
        return ret  
    ans = float('inf')
    n = int(input())
    a = divisor(n)
    for item in a:
        ans = min(ans, item-1+n//item-1)
    print(ans)
abc144c_walk_on_multiplication_table()

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

for i in range(1,int(n**0.5)+1):
    if n%i != 0: continue

    j = n//i
    ans = min(ans,(i-1)+(j-1))

print(ans)

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

Solution (Python):
n = int(input())
c = n-1
for i in range(1,int(n**0.5)+1):
  if (n/i).is_integer():
    j = n//i
    c = min(c, i+j-2)
print(c)

Solution (Python):
from math import sqrt as s

N = int(input())
x, y = [], []
for i in range(1, int(s(N))+1):
  if N % i == 0:
    x.append(i)
    y.append(N // i)

ans, M = float("INF"), len(x)
for i in range(M):
  ans = min(ans, x[i] + y[i] - 2)

print(ans)

Solution (Python):
from math import sqrt as s

N = int(input())
x, y = [], []
for i in range(1, int(s(N))+1):
  if N % i == 0:
    x.append(i)
    y.append(N // i)

ans, M = float("INF"), len(x)
for i in range(M):
  ans = min(ans, x[i] + y[i] - 2)

print(ans)

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

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

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

Solution (Python):
N=int(input())
ans=N-1
x=1
while x*x<=N:
    if N%x==0:
        y=N//x
        ans=min(x-1+y-1,ans)
    x+=1
print(ans)

Solution (Python):
N = int(input())
a = 0
for i in range(1,int(N**0.5)+1):
    if N%i == 0:
        a = max(a,i)
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())

yakusu_list = make_divisors(N)
if len(yakusu_list)%2 == 1:
    ans = (yakusu_list[len(yakusu_list)//2]-1)*2
else:
    ans = yakusu_list[len(yakusu_list)//2-1] - 1 + yakusu_list[len(yakusu_list)//2] - 1

print(ans)

Solution (Python):
import math
N = int(input())
M = []
sqr_N = math.floor(math.sqrt(N))
a = 0
    
for i in range(1, (sqr_N + 1)):
    if N % i == 0:
        a = i

a_pair = N // a
print(a + a_pair - 2)

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

top = int(math.sqrt(n))
a = 1

for x in range(1,top+1):
  if n % x == 0:
    a = x
    
a_pair = n // a
print(a + a_pair -2)

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

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

Solution (Python):
def make_divisors(n):
    '''
    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]


def solve():
    N = int(input())
    D = make_divisors(N)
    ans = N - 1
    for d in D:
        ans = min(ans, N // d - 1 + d - 1)
    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):




def make_divisors(n: int):
    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]



def resolve():
    import sys
    input = sys.stdin.readline

    n = int(input().rstrip())
    lower_divisors, _ = make_divisors(n)
    print(min(lower_divisor + (n // lower_divisor) - 2 for lower_divisor in lower_divisors))





if __name__ == "__main__":
    resolve()

Solution (Python):
import math

n = int(input())

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

def check(x):
  res = 10 ** 12
  for i in range(2,int(math.sqrt(x))+1):
    if x % i == 0:
      s = (i-1) + (x//i -1)
      res = min(res,s)
  return res

if p(n):
  print(n-1)
else:
  print(check(n))

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

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

Solution (Python):
import math

def main():
    N = int(input())
    ans = N
    for i in range(1, int(math.sqrt(N)) + 1):
        if N % i != 0:
            continue
        j = N // i
        d = (i - 1) + (j - 1)
        if d < ans:
            ans = d
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
N = int(input())
mini = float('inf')

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

print(mini - 2)

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

print(m-2)

Solution (Python):


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

Solution (Python):


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

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







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

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

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

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)
        else:
          continue
print(ans)

Solution (Python):

"""
Created on Wed Sep  9 01:55:56 2020

@author: liang
"""

import math
N = int(input())
key = int(math.sqrt(N))

ans = 10**12
for i in reversed(range(1,key+1)):
    if N%i == 0:
        ans = i-1 + N//i -1
        break
print(ans)

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

    n = int(readline())
    a = int(n**(1/2)//1)
    for i in range(a, 0, -1):
        b, m = divmod(n, i)
        if m == 0:
            print(i+b-2)
            exit()

if __name__ == '__main__':
    main()

Solution (Python):
def div(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]

def main():
  import functools
  import operator
  import numpy as np
  import itertools
  import math
  lis = [1]
  N = int(input())
  x = div(N)
  A = []
  for i in range(math.ceil(len(x)/2)):
    A.append(x[i]+x[len(x)-i-1])
  ans = min(A)-2
  print(ans)
main()

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

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

Solution (Python):
N = int(input())
d = 10**20
for a in range(1,int(N ** 0.5)+1):
  if N % a == 0:
    b = N // a
    c = a + b - 2
    if d > c:
      d = c
print(d)

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

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

sqrt_n = int(math.sqrt(n))
num_i = 0
for i in range(1, sqrt_n+1):
    if n % i == 0:
       num_i = i

num_j = n // num_i
ans = (num_i - 1) + (num_j - 1)

print(ans)

Solution (Python):
import math

ans = 1000000000000000000
N = int(input())
for i in range(math.ceil(math.sqrt(N+1000))):
  if i > 0 and N % i == 0:
    ans = min(i + N/i - 2, ans)
print(int(ans))

Solution (Python):
import math

N = int(input())
ans = 10**12

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

print(ans)

Solution (Python):


from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


def solve(N: int):
    def manhattan_distance(x1, y1, x2, y2):
        return abs(x1-x2) + abs(y1-y2)
    return min(manhattan_distance(1, 1, i, N//i) for i in range(1, int(math.sqrt(N))+1) if N % i == 0)


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    print(f'{solve(N)}')


if __name__ == '__main__':
    main()

Solution (Python):
import sys

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]

input = sys.stdin.readline
n = int(input())

yaku_list = make_divisors(n)
index = int(len(yaku_list)/2)


if len(yaku_list) % 2 == 0:
    result = yaku_list[index-1] - 1 + yaku_list[index] - 1 
else:
    result = yaku_list[index] - 1 + yaku_list[index] - 1 

print(result)

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

Solution (Python):
n=int(input())
ans=float('inf')
a=1
while n**0.5>=a:
  if n%a==0:
    ans=min(ans,a+n//a-2)
  a+=1
print(ans)

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

ans = float("inf")
for i in range(1, int(n**0.5)+1):
  if n%i == 0:
    t = n//i
    if i-1+t-1 < ans: ans = i-1+t-1
print(ans)

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

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

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

Solution (Python):
import math

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

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

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

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

def main():
    mod=10**9+7
    N=I()
    ans=N
    
    M=10**6+5
    for i in range(1,M):
        if N%i==0:
            j=N//i
            temp=i+j-2
            ans=min(ans,temp)
            
    print(ans)
            
        

main()

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

    ans = 1e18
    for i in range(1, N + 1):
        if i * i > N:
            break
        if N % i != 0:
            continue
        j = N // i
        ans = min(ans, i + j - 2)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
x = int(N**0.5)+1
ans_1 = N

for i in range(x):
    if N % x == 0:
        ans_1 = x
        break
    else:
        x -= 1

ans_2 = N//ans_1
print(ans_1 + ans_2 - 2)

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

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

    print(ans)


if __name__ == "__main__":
    main()

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

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

if cnt == 0:
  ans = n-1
print(int(ans))

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    def enum_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

    divs = enum_divisors(N)

    ans = INF
    for div1 in divs:
        div2 = N // div1
        score = (div1 + div2 - 2)
        ans = min(score, ans)

    print(ans)


if __name__ == '__main__':
    main()

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

divisors = []

i = 1
while i*i <= N:
    if N % i == 0:
        divisors.append(i)
    i += 1

print(divisors[-1] + N // divisors[-1] - 2)

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

Solution (Python):


import math

n = int(input())
a = int(math.sqrt(n))
b = 0
while b == 0:
    if n % a > 0:
        a -= 1
        continue
    b = n / a

res = a + b - 2
print(int(res))

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


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

Solution (Python):
import math
N=int(input())
a=1
sum=N
flag=True
while flag:
    if a>math.sqrt(N):
        break
    if N%a==0:
        b=N//a
        if sum>a+b-2:
            sum=a+b-2
    a+=1
print(sum)

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]
mdn = make_divisors(N)

aa = []
for x in mdn:
  aa.append(x+N//x)
print(min(aa)-2)

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n = I()
def yaku(m):
    ans = []
    i = 1
    while i*i <= m:
        if m % i == 0:
            j = m // i
            ans.append(i)
        i += 1
    ans = sorted(ans)
    return ans
a = yaku(n)

ans = float('inf')
for v in a:
    ans = min(ans, (v-1)+(n//v)-1)
print(ans)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

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







n = I()

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]

ans = make_divisors(n)


m = INF
for i in range(len(ans)//2+1):
    c = ans[i] - 1 + ans[-i-1] - 1
    
    m = min(m,c)
print(m)

Solution (Python):
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):
def enum_divisors(n):
    res = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            res.append(i)
    return res


N = int(input())
divs = enum_divisors(N)
ans = float("inf")
for div in divs:
    ans = min(ans, div + N // div - 2)
print(ans)

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

    divisors.sort()
    return divisors

d = make_divisors(n)
ans = 10**18
for a in d:
    ans = min(ans, n//a-1+a-1)
print(ans)

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
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()
for i in range(int(N**.5),0,-1):
    if N % i == 0:
        print(i+(N//i)-2)
        exit()

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

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

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