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):
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())
x = make_divisors(n)

if len(x) % 2 == 0:
    a = x[int((len(x)-1)/2)]
    b = x[int((len(x)+1)/2)]
else:
    a = x[int(len(x)/2)]
    b = x[int(len(x)/2)]

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

t = make_divisors(N)
if len(t) % 2 != 0:
    i = len(t) // 2
    print(t[i] + t[i] - 2)
else: 
    i = len(t) // 2 - 1
    print(t[i] + t[i+1] - 2)

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

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

    return divsets

divs = make_divsets(N)


min = 1000000000000019

for (a, b) in divs:
    steps = a-1 + b-1
    if steps < min:
        min = steps
print(min)

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

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

i = 1
u = [[1, N]]
while i ** 2 <= N:
  if N / i == N // i:
    u[0] = [i, N // i]
  i += 1
    
print(u[0][0] + u[0][1] - 2)

Solution (Python):
import numpy as np
N=int(input())
root=int(np.sqrt(N)//1)
a=N
b=N
for i in range(root, 0, -1):
    if N%i==0:
        a=i
        b=N//i
        break
print(a-1+b-1)

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

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

Solution (Python):
N = int(input())
lis = []
for i in range(1,10**6+1):
  a = N/i
  if a.is_integer():
    lis.append(N/i+i-2)
if len(lis) == 0:
  print(N-1)
else:
  print(int(min(lis)))

Solution (Python):
import math

A = int(input())

start = math.ceil(math.pow(A, 0.5))
while A % start != 0:
    start -= 1

print((start + (A // start) - 2))

Solution (Python):
import math

n = int(input())
counter = math.ceil(math.sqrt(n))
while True:
    if n % counter == 0:
        break
    counter -= 1
print(counter + (n // counter) - 2)

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

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

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

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

Solution (Python):
def run(N):
    lists = [N-1]
    for i in range(2, int(N**(1/2))+1):
        if N % i == 0:
            lists.append(i + (N // i) - 2)
    return min(lists)


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


if __name__ == '__main__':
    main()

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

Solution (Python):
import math
n=int(input())
b=int(math.sqrt(n))
for i in range(1,b+1)[::-1]:
    if n/i==n//i:
        c=i
        d=n//i
        break
print(c+d-2)

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

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

l = n - 1

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

Solution (Python):
import math

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

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

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

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

Solution (Python):
n = int(input())
cost = []
i = 2
while i <= n**0.5:
  if n % i == 0:
    cost.append(i+n/i-2)
  i += 1
if len(cost) == 0:
  cost.append(n-1)
print(int(min(cost)))

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
  
A= input()
A= int(A)
X = 100000000000000000000000
for i in make_divisors(A):
  Y = A / i
  if Y + i < X:
    X = Y + i
print(int(X-2))

Solution (Python):
n = int(input())
ans = float('inf')
for i in range(1, n):
    if i**2 > n:
        break
    if n % i ==0:
        tmp = i + (n//i) -2
        if tmp<ans:
            ans = tmp
print(ans)

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

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

l = [i for i in range(1, int(N**0.5) + 1) if N % i == 0]
n = int(N / l[-1])
print(l[-1] + n - 2)

Solution (Python):

def nums():
    return list(map(int,input().split(" ")))
def num():
    return int(input())
def yakusu(n):
    res = set()
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            res.add((i,int(n/i)))
    return res
"""
a,b = nums()
if a <= 9 and b <= 9:
    print(a * b)
else:
    print(-1)
"""
"""
n = num()
qq = []
for i in range(10):
    for j in range(10):
        qq.append(i * j)
if n in qq:
    print("Yes")
else:
    print("No")
"""
N = num()
y = yakusu(N)

min = -1

for i in y:
    x,y = i
    if min == -1:
        min = x + y
    elif min > x + y:
        min = x+y

print(min-2)

Solution (Python):
from fractions import gcd

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

divs = make_divisors(N)

min_cnt = 10 ** 13
for d in divs:
    
    x = N // d
    if (d + x - 2) < min_cnt:
        min_cnt = d + x - 2
print(min_cnt)

Solution (Python):
from math import sqrt
import sys
def input(): return sys.stdin.readline().strip()

def main():
    n = int(input())
    i = int(sqrt(n))
    while i > 0:
        if n % i == 0:
            print(i - 1 + (n // i) - 1)
            return
        i -= 1
main()

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

N = int(input())

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

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

def get_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 = get_divisors(N)
divs.sort()
if len(divs)%2==1:
    idx = (len(divs)-1) // 2
    print(2 * (divs[idx] - 1))

else:
    idx = len(divs)//2 - 1
    print(divs[idx] - 1 + divs[idx + 1] - 1)

Solution (Python):
import sys
import math
import os

input = sys.stdin.readline

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


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

b = n//a
ans = a + b -2
print(ans)

Solution (Python):

import math

def f(n):
    min_ = float("inf")
    end = int(math.sqrt(n) + 1)
    for div in range(1, end + 1):
        if n % div == 0:
            r = n // div
            min_ = min(min_, (div - 1) + (r - 1))
    return min_

assert f(10) == 5
assert f(50) == 13
assert f(10000000019) == 10000000018

n = int(input())
ans = f(n)
print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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

def main():
  n = ni()

  x = divi(n)
  
  ans = 1e18+7
  for i in range(0, len(x), 2):
    
    try:
      ans = min(ans, x[i] - 1 + x[i+1] - 1)
    except:
      ans = min(ans, (x[i] - 1) * 2)  
  print(ans)
  
main()

Solution (Python):
import numpy as np
import sys

N = int(input())

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

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

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

print(m - 2)

Solution (Python):
import math

N=int(input())
SQ=math.floor(math.sqrt(N))

for i in range(SQ):
    if N%(SQ-i)==0:
        p=(SQ-i)
        q=N//(SQ-i)
        break
print(p+q-2)

Solution (Python):


import sys
import math
import collections



n=int(input())




sta=math.floor(math.sqrt(n))



for i in range(sta):
	if(n%(sta-i)==0):
		print(n//(sta-i)+sta-i-2)
		sys.exit()

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

x = -1
y = -1
for i in reversed(range(1, int(N**0.5) + 1)):
    if N % i == 0:
        x = i
        y = N // i
        break

walk_count = x - 1 + y - 1
print(walk_count)

Solution (Python):
import math

N = int(input())

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

print(cell-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)
            divisors.append(n//i)
    return divisors
    
divisors = make_divisors(N)

x_ax = divisors[-2] - 1
y_ax = divisors[-1] - 1

print(x_ax + y_ax)

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

if __name__ == "__main__":
    f_c()

Solution (Python):
from math import sqrt

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

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


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys
import math

n = int(input())

sqrtn = int(math.sqrt(n))

for i in range(sqrtn, 0, -1):
	if n % i == 0:
		res = int((n / i) - 1 + i - 1)
		print(res)
		sys.exit()

Solution (Python):
import math

def read():
    N = int(input().strip())
    return N, 

def solve(N):
    pairs = []
    n = math.ceil(math.sqrt(N)) + 1
    for i in range(1, n):
        j = N // i
        if i * j == N:
            pairs.append((i, j))
    min_moves = 1000000000000
    for pair in pairs:
        min_moves = min((pair[0] - 1) + (pair[1] - 1), min_moves)
    return min_moves

if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Solution (Python):
n = int(input())
import math
m = int(math.sqrt(n))+1
anss = []
for x in range(1, m+1):
  y = n//x
  z = n%x
  if z == 0:
    anss.append(abs(x-1)+abs(y-1))
print(min(anss))

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

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

divs = divisors(n)

ans = n-1
for div in divs:
    div2 = n//div
    ans = min(ans, div + div2 - 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)

    
    return divisors

N = int(input())
minimam = 10**12
for x in make_divisors(N)[::2]:
    minimam = min(minimam, int(x + N/x - 2))
print(minimam)

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

ans = N-1

def get_steps(a, b):
    return (a-1) + (b-1)


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

print(ans)

Solution (Python):
def resolve():
    N = int(input())
    i = 1
    I = 1
    J = N
    while(i*i <= N):
        if N % i == 0:
            I = i
            J = N // i
        i += 1
    print(I + J - 2)
resolve()

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

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

Solution (Python):
import sys

def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

def all_factors(n):
    """returns a sorted list of all distinct factors of n"""
    small, large = [], []
    for i in range(1, int(n**0.5) + 1, 2 if n & 1 else 1):
        if not n % i:
            small.append(i)
            large.append(n // i)
    if small[-1] == large[-1]:
        large.pop()
    large.reverse()
    small.extend(large)
    return small

def solve(a,b):
    return (a-1)+(b-1)


n = int(input())
facts = all_factors(n)
length = len(facts)
mini = 10**15 + 7
if length&1:
    for i in range(length//2+1):
        mini = min(mini, solve(facts[i], facts[~i]))
    print(mini)
else:
    for i in range(length//2):
        mini = min(mini, solve(facts[i], facts[~i]))
    print(mini)

Solution (Python):
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())

x = ii()

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
y = make_divisors(x)

m = 10000000000000

if(len(y)%2==1):
    for i in range(0,len(y),2):
        if(i==len(y)-1):
            if m > (y[i]-1)*2:
                m = (y[i]-1)*2
        else:
            if m > y[i]+y[i+1]-2:
                m = y[i]+y[i+1]-2
else:
    for i in range(0,len(y),2):
        if m > y[i]+y[i+1]-2:
                m = y[i]+y[i+1]-2

print(m)

Solution (Python):
import math

N = int(input())

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

ans = (a-1) + (int(N/a)-1)

print(ans)

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

n = int(input())
ans = float('inf')
for i in make_divisors(n):
    j = n//i
    temp = i + j - 2
    ans = min(ans, temp)
print(ans)

Solution (Python):

from collections import  Counter

n=int(input())

ans=10**12+1

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(table)
    table.sort()
    return table

t=divisor(n)
for i in range(0,len(t)//2):
    tmp=t[i]+(n//t[i])-2
    ans=min(ans,tmp)

print(ans)

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

a = int(n ** 0.5)

ans = n - 1

for i in range(2, a + 1):
    if n % i == 0:
        ans = i + n //i - 2

print(ans)

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

Solution (Python):

from __future__ import division, print_function

import os
import sys
import math
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


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

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




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

q = int(math.floor(math.sqrt(N)))
ret = float("inf")
for i in range(1, q + 1):
	if N % i == 0:
		ret = min(i + int(N / i), ret)
		
		
print(ret - 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
n = int(input())
a = make_divisors(n)
b = [ int(i + n / i)  for i in a]


ans = min(b) - 2
print(ans)

Solution (Python):
from collections import *
from math import *
n = int(input())
x = int(sqrt(n))
for i in range(x,0,-1):
    if(n%i == 0):
        print(i-1+n//i-1)
        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)

    divisors.sort()
    return divisors
a = int(input())
b = make_divisors(a)
b_len = len(b)
if b_len % 2 == 0:
    print(b[b_len//2-1] + b[b_len//2] - 2)
else:
    print(2 * b[b_len//2 ] - 2)

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

n = int(input())

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

Solution (Python):
N = int(input())
def make_div(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 sorted(divisors)
div = make_div(N)
minn = 100000000000000000000
for i in range(len(div)):
    a,b = div[i],div[-i-1]
    if a > b:
        break
    if (a + b) < minn:
        minn = a + b

print(minn-2)

Solution (Python):
N=input()

N_sqrt=int(N**0.5)+1

S=set()
S.add(1)
S.add(N)

for i in range(1,N_sqrt+1):
    if N%i==0:
        S.add(i)
        S.add(N/i)


ans=float("inf")
for x in S:
    y=N/x
    ans=min(ans,x+y-2)

print ans

Solution (Python):
def main():
    N = int(input())
    i = 1
    count = 10000000000000
    while i * i <= N:
        if N % i == 0:
            A = N / i
            B = N / A
            count = min(count, (A - 1) + (B - 1))
        i += 1
    print(int(count))


if __name__ == "__main__":
    main()

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

print(a + b - 2)

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

Solution (Python):
def main():
    n = int(input())
    x = 10 ** 13
    i = 1
    while i * i <= n:
        if n % i == 0:
            j = n // i
            x = min(x, (i - 1) + (j - 1))
        i += 1
    print(x)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
leng=10**100

mx=int(n**0.5)+2

for i in range(1,mx):
    j=n/i
    if j.is_integer():
        tmp=i+j-2
        if tmp<leng:
            leng=tmp

print(int(leng))

Solution (Python):
import math

if __name__ == "__main__":
	N = int(input())
	ans = 1000000000000
	for i in range(int(math.sqrt(N)) + 1, 0, -1):
		if N % i == 0 and i + (N // i) - 2 < ans:
			ans = i + (N // i) - 2

	print(ans)

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

Solution (Python):
import math

N = int(input())
root = math.sqrt(N)
pair = []

for i in range(math.floor(root)):
    I = i + 1
    if N % I == 0:
        pair.append([I, N/I])
        
wa = []
        
for i in range(len(pair)):
    wa.append((pair[i][0] - 1) + (pair[i][1] - 1))
else:
    print(int(min(wa)))

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

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, i + N//i)

print(ans-2)

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

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

Solution (Python):
from fractions import gcd
from collections import defaultdict

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

divs = make_divisors(N)

res = N+1

for div in divs:
    res = min(N+1, div+N//div)

print(res-2)

Solution (Python):
import math

n = int(input())
ans = 10**18
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 sys


input = sys.stdin.readline


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

    ary = []
    n = 1
    while n**2 <= N:
        if N % n == 0:
            ary.append(n)
        n += 1

    ans = 99999999999999999999999
    for i in ary:
        ans = min(ans, i+N//i-2)
    print(ans)


if __name__ == '__main__':
    main()

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

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


def main():
    N = ii()
    divs = [i for i in gen_divs(N)]
    divs.sort()
    if len(divs) % 2 == 1:
        print((divs[len(divs)//2]-1)*2)
    else:
        p = len(divs) // 2
        print(divs[p]-1 + divs[p-1]-1)




main()

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

    
    return divisors

res = float('inf')
for x in make_divisors(N):
    res = min(res, x + N//x - 2)
print(res)

Solution (Python):
import math
n = int(input())
n1 = int(math.sqrt(n))
cnt = 10**18
for i in range(1, n1+1):
  if n % i == 0:
      j = (n/i) - 1
      ans = i-1
      cnt = min(cnt, j+ans)
      
  else:
    continue
print(int(cnt))

Solution (Python):
N=int(input())
import copy
n=copy.deepcopy(N)
D={}
for i in range(2,10**6+1):
    while N%i==0:
        if i not in D:
            D[i]=1
            N//=i
        else:
            D[i]+=1
            N//=i
if N!=1:
    D[N]=1
D=list(D.items())

ANS=[1]
for i in range(len(D)):
    ans=[]
    for j in ANS:
        for k in range(D[i][1]+1):
            ans.append(j*(D[i][0]**k))
    
    ANS=ans


cnt=10**12
for i in ANS:
    if (i+(n//i))-2<cnt:
        cnt=(i+(n//i))-2
print(cnt)

Solution (Python):
N = float(input())
A = int(N**(0.5))
max = 10**12
for i in range(1,A+1):
    B = N/float(i)
    if B.is_integer():
        c = i+B-2
        if c<= max:
            max = c
print(int(max))

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines























def main():
	n=int(input())
	nn=int(n**0.5)
	ans=[]
	
	for i in range(nn+1,0,-1):
		if n%i==0:
			ans.append(i)
			ans.append(n//i)
			break
	print(sum(ans)-2)
	
	




if __name__ == '__main__':
	main()

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

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

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

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

print(ans)

Solution (Python):
import math
import sys

N = int(input())

m = int(math.sqrt(N))

for i in range(m+1, 0, -1):
    if N % i == 0:
        print(i-1 + N//i-1)
        sys.exit()

Solution (Python):
import math

n = int(input())

ans = n-1

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):
import math
N = int(input())
m = 0
for i in range(1, int(math.sqrt(N)+1)):
    if N % i == 0:
        m = i

l = N // m
print(m+l-2)

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

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

res = 10**13
for y in yakusu:
    x = N//y
    dist = (y-1)+(x-1)
    res = min(res, dist)

print(res)

Solution (Python):
import math
import sys

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

for i in range(num):
    if n % (num-i) == 0:
        print((num-i) + (n//(num-i)) - 2)
        sys.exit()

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

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

sN = int(N ** 0.5)

result = N - 1
for n in range(1, sN+1):
    if N % n == 0:
        result = min(result, n-1 + (N//n) -1)
print(result)

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

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

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

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

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

    
    return divisors


k=make_divisors(n)
if len(k)==2:
    print(sum(k)-2)
else:
    print(k.pop()*2-2)