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
sys.setrecursionlimit(10000000)


def main():
    N = int(input())
    min_ = N - 1
    for i in range(1, int(N ** 0.5) + 1):
        if N % i != 0:
            continue
        else:
            j = N // i
            min_ = min(min_, i + j - 2)
    print(min_)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ans = 10**12 + 1
import math
for i in range(1, math.ceil(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())
dividers = []

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

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

    divisors.sort()
    return divisors

N=int(input())
arr=make_divisors(N)


n=N**(0.5)

min_s=10**10
min_a=-1
for i,a in enumerate(arr):
  if min_s>abs(a-n):
    min_a=a
    min_s=abs(a-n)
print(min_a+N//min_a-2)

Solution (Python):





def main():
  N = int(input())
  ret = N-1
  for i in range(1,int(N**0.5)+1):
    if N%i != 0:
      continue
    else:
      ret = min(ret,i+N//i-2)
  return ret

print(main())

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

print(min(ans))

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

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

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

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

divs = []

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

ans = 1000000000007

for i, j in divs:
    ans = min(ans, i-1+j-1)

print(ans)

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

Solution (Python):
def divisor(n):
    ass = float('inf')
    for i in range(1,int(n**0.5)+1):
        tmp = 0
        if n%i == 0:
            tmp += i
            tmp += n//i
            ass = min(ass, tmp)
    return ass

def main():
    N = int(input())
    tmp = divisor(N)
    print(tmp-2)

  

if __name__ == "__main__":
    main()

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

N = int(input())

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

l = divisor(N)

s = 0
t = len(l)-1
while s < t:
    s += 1
    t -= 1

print(l[s]+l[t]-2)

Solution (Python):
def main():
    N = int(input())
    end = int(N ** 0.5)
    ans = 100100100100100
    for i in range(1, end+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())
for c in range(int(n**0.5),0,-1):
    if n%c==0:
        d=int(n/c)
        break
print(c+d-2)

Solution (Python):
import math
import sys
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()

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

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

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

class Prime:
    seed_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

    def is_prime(self, n):
        """
        prime test (hybrid)

        see also: https://qiita.com/gushwell/items/ff9ed83ba55350aaa369

        :param n:
        :return: boolean
        """
        is_prime_common = self.is_prime_common(n)
        if is_prime_common is not None:
            return is_prime_common

        if n < 2000000:
            return self.is_prime_brute_force(n)
        else:
            return self.is_prime_miller_rabin(n)

    def is_prime_common(self, n):
        if n == 1: return False
        if n in Prime.seed_primes: return True
        if any(map(lambda x: n % x == 0, self.seed_primes)): return False

    def is_prime_brute_force(self, n):
        """
        brute force prime test
        use with is_prime_common if you want to skip seed_primes

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

    def is_prime_miller_rabin(self, n):
        """
        miller rabin prime test
        use with is_prime_common if you want to skip seed_primes

        see also
            algorithm: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
            implementation: https://qiita.com/srtk86/items/609737d50c9ef5f5dc59
            improvement: https://qiita.com/gushwell/items/ff9ed83ba55350aaa369

        :param n:
        :return: boolean
        """

        d = n - 1
        while d & 1 == 0:
            d >>= 1

        
        witnesses = self.get_witnesses(n)
        

        for w in witnesses:
            y = pow(w, d, n)

            while d != n - 1 and y != 1 and y != n - 1:
                y = (y * y) % n
                d <<= 1

            if y != n - 1 and d & 1 == 0:
                return False

        return True

    def get_witnesses(self, num):
        def _get_range(num):
            if num < 2047:
                return 1
            if num < 1373653:
                return 2
            if num < 25326001:
                return 3
            if num < 3215031751:
                return 4
            if num < 2152302898747:
                return 5
            if num < 3474749660383:
                return 6
            if num < 341550071728321:
                return 7
            if num < 3825123056546413051:
                return 9
            return 12

        return self.seed_primes[:_get_range(num)]

    def gcd(self, a, b):
        if a < b:
            return self.gcd(b, a)
        if b == 0:
            return a
        while b:
            a, b = b, a % b
        return a

    @staticmethod
    def f(x, n, seed):
        """
        pseudo prime generator
        :param x:
        :param n:
        :param seed:
        :return: pseudo prime
        """
        p = Prime.seed_primes[seed % len(Prime.seed_primes)]
        return (p * x + seed) % n

    def find_factor(self, n, seed=1):
        """
        find one of factor of n
        this function is based to Pollard's rho algorithm

        see also
            algorithm: https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
            implementation: https://qiita.com/gushwell/items/561afde2e00bf3380c98

        :param n:
        :param seed:
        :return: factor
        """
        if self.is_prime(n):
            return n

        x, y, d = 2, 2, 1
        count = 0
        while d == 1:
            count += 1
            x = self.f(x, n, seed)
            y = self.f(self.f(y, n, seed), n, seed)
            d = self.gcd(abs(x - y), n)

        if d == n:
            return self.find_factor(n, seed + 1)
        return self.find_factor(d)

    def find_factors(self, n):
        primes = {}
        if self.is_prime(n):
            primes[n] = 1
            return primes

        while n > 1:
            factor = self.find_factor(n)

            primes.setdefault(factor, 0)
            primes[factor] += 1

            n //= factor

        return primes

prime = Prime()
if prime.is_prime(n):
    print(n - 1)
else:
    for i in range(math.ceil(math.sqrt(n)),0,-1):
        if n % i == 0:
            break
    print(i + n // i - 2)

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

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

    
    return divisors

N = int(input())

vD = make_divisors(N)


res = min(d + N//d - 2 for d in vD)
print(res)

Solution (Python):
def make_divisors(n):
    divisors =  float('inf')
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors = min(divisors,i + n//i -2)
    
    return divisors


n = int(input())

distance = make_divisors(n)
print(distance)

Solution (Python):
import math

n = int(input())
for i in range(math.ceil(n**0.5), 0, -1):
    if n % i == 0:
        a, b = i, n//i
        break

print(a+b-2)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from math import sqrt

def get_yakusu(n):
    i = 1
    ans = []
    while i**2 <= n:
        if n % i == 0:
            ans.append((i, n//i))
        i += 1
    return ans

def main():
    n = int(input().strip())
    y = get_yakusu(n)
    ans = 10 ** 20
    for a, b in y:
        ans = min(ans, a-1+b-1)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):

import sys, math


def solve(N: int):
    x = 1
    for i in range(1, math.ceil(math.sqrt(N))+1):
        m = N // i
        d = N % i
        if d == 0:
            x = max(1, i)
    print(x+N//x-2)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

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

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

border = int(N**0.5)
for x in reversed(range(1,border+1)):
    if N % x == 0:
        goal = [x, N//x]
        break

print(sum(goal)-2)

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

    
    return divisors

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

minimum=N*2
for elem in div:
    A=elem
    B=N//elem
    if(A+B)<minimum:
        minimum=A+B
print(minimum-2)

Solution (Python):
import math

def divisor(num):
    if num < 0:
        return 0
    if num == 1:
        return [1, ]

    divisor_list = [1, ]
    divisor_list_r = [num, ]

    i = 2
    while i * i <= num:
        if num % i == 0:
            divisor_list.append(i)
            
        i += 1
    return divisor_list
    


N = int(input())
div_list = divisor(N)
ans = div_list[-1] + N // div_list[-1] - 2
print(ans)

Solution (Python):
import math

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

for x in range(n, -1, -1):
    if N % x == 0:
        out = (N // x) + x - 2
        break

print(out)

Solution (Python):


import math


def solve(n):
    dmax = 0
    for d in range(1, int(math.sqrt(n)) + 1):
        if n % d == 0:
            dmax = max(dmax, d)
    cmax = n // dmax
    res = (cmax - 1) + (dmax - 1)
    return res


def main():
    n = int(input())
    res = solve(n)
    print(res)


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
n = int(input())
min_c = n - 1

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

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

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


def main():
    import math
    import collections
    import itertools

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



if __name__ == '__main__':
    main()

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

Solution (Python):
def make_divisors(n):
    """自然数nの約数を列挙したリストを出力する
    計算量: O(sqrt(N))
    入出力例: 12 -> [1, 2, 3, 4, 6, 12]
    """
    divisors = []
    for k in range(1, int(n**0.5) + 1):
        if n % k == 0:
            divisors.append(k)
            if k != n // k:
                divisors.append(n // k)
    divisors = sorted(divisors)
    return divisors


n = int(input())
ans = make_divisors(n)
res = 10**18
for i in range(len(ans)):
    tmp = ans[i] + n // ans[i] - 2
    res = min(tmp, res)
print(res)

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

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

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

if __name__ == '__main__':
    print(solve())

Solution (Python):
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline


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

Solution (Python):
N = int(input())
rs = []
for n in range(1, N+1):
  if n*n > N:
    break
  if N%n == 0:
    r = n+N//n-2
    rs.append(r)
r = min(rs)
print(r)

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

Solution (Python):
N = int(input())
k = 1
while True :
    if k * k > N :
        break
    else:
        k += 1
for i in range(k,0,-1):
    if N % i == 0 :
        a = i
        b = N // i
        print(a + b -2)
        exit()

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

Solution (Python):
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

N = INT()

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

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

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

    divisors.sort()
    l = len(divisors)
    if l % 2 == 0:
        print(divisors[l//2] + divisors[l//2-1] - 2)
    else:
        print(2 * divisors[l//2]- 2)


if __name__ == '__main__':
    main()

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

import math
for a in range(int(math.sqrt(n)) + 1, 0, -1):
  b = n // a
  if a * b == n:
    print(a - 1 + b - 1)
    exit(0)

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
input = sys.stdin.readline

setrecursionlimit(1000000)

dp = defaultdict(lambda x:-1)

def main():
	n = int(input())
	i = int(math.sqrt(n)) + 1
	while True:
		if(n % i == 0):
			print(n//i + i - 2)
			return
		i -= 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 zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

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

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

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


n = int(input())
divs = make_divisors(n)
print(min(divs)-2)

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

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

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

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

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

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

Solution (Python):
def getDivisors(x):
    anss = set()
    for d in range(1, int(x**0.5)+1):
        if x%d == 0:
            anss.add(d)
            anss.add(x//d)
    return sorted(anss)

N = int(input())

divs = getDivisors(N)

ans = N
for div in divs:
    num = div + N//div - 2
    ans = min(ans, num)

print(ans)

Solution (Python):
a = int(input())
imin = 999999999999999
for number in range(1, int(a ** 0.5 + 1)):
    if a % number == 0:
        imin = min(imin, number + a // number - 2)
print(imin)

Solution (Python):
from math import sqrt

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

print(ans)

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

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

p = int(n ** 0.5)

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

print(n-1)

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input())

a = int(N**0.5)

b = -1
while b==-1:
	if N%a==0:
		b = N//a
		break
	a -= 1

ans = a+b-2
print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

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

Solution (Python):
n=int(input())
arr=[1]
from math import sqrt as S 
for i in range(2,int(S(n))+1):
    if n%i==0: 
        arr.append(i)
mini=10**20 
for a in arr: 
    b=n//a 
    mini=min(mini,a+b-2)
print(mini)

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

Solution (Python):
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n = INT()

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

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

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


def resolve():
    n = int(input())
    l = make_divisors(n)
    print(min([x + (n // x) for x in l]) - 2)


if __name__ == '__main__':
    resolve()

Solution (Python):






from sys import stdin,setrecursionlimit







setrecursionlimit(10**6)
input=stdin.readline

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

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

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

Solution (Python):
n=int(input())
def 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 divisors
ans=div(n)
ans.sort()
m=len(ans)
if(m%2==1):
    a=ans[m//2]
    b=ans[m//2]
else:
    a=ans[m//2-1]
    b=ans[m//2]
print(a+b-2)

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

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

Solution (Python):

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors
n=int(input())
lst=make_divisors(n)
ans=float("inf")
for u in lst:
    ans=min(ans,u+n//u-2)
print(ans)

Solution (Python):
def divisors(num):
    array = []
    limit = int(num ** 0.5) + 1
    for i in range(1, limit):
        if num % i == 0:
            div1 = i
            div2 = num//i
            array.append(div1+div2-2)
    array.sort()
    return array

n = int(input())
ans = min(divisors(n))
print(ans)

Solution (Python):
from math import *

n = int(input())
sn = int(sqrt(n))
a, b = 0, 0

for x in range(sn, 0, -1):
    if n % x == 0:
        print(x + n // x - 2)
        break

Solution (Python):
def main():

    n = int(input())
    cnt = n
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            cnt = min(cnt, i + n / i - 2)
    print(int(cnt))
    
if __name__ == "__main__":
    main()

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

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

print(ans-2)

Solution (Python):


N=int(input())

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

Solution (Python):




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

  for i in range(1, n):
    if i ** 2 > n:
      break

    if n % i != 0:
      continue

    s = min((i - 1) + (n // i - 1), s)

  print(s)


if __name__ == '__main__':
  main()

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

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

    divisors.sort()
    return divisors

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

    divisors = make_divisors(N)

    mi = float("inf")
    for v in divisors:
        x = N // v
        mi = min(mi, x + v - 2)

    print(mi)

    

if __name__ == '__main__':
    main()

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


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

    return min(divisors)


print(solve(N))

Solution (Python):
def main():
    n=int(input())
    def yakusuu(a):
        yakusuu=set()
        for i in range(1,int(a**0.5)+1):
            if a%i==0:
                yakusuu.add(i)
                yakusuu.add(a//i)
        yakusuu=sorted(list(yakusuu))
        return yakusuu
    y=yakusuu(n)
    print(min([i+(n//i)-2 for i in y]))
        
main()

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

Solution (Python):

N = int(input())

i = 1
min_num = 10000000000000000
while i <= N**0.5 + 1:
	if N % i == 0:
		min_num = min(min_num, i + N//i)
	i += 1

print(min_num - 2)

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

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

Solution (Python):

n = int(input())
divs = []
for i in range(1, int(n**0.5) + 1):
    if n % i == 0:
        divs.append(i)
d = divs[-1]
print(d - 1 + n // d - 1)

Solution (Python):
N = int(input())
ans = 10**13
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().strip())
ans = float('inf')
for i in range(1, int(N**0.5+1)):
  if N % i == 0:
    ans = min(ans, i+N//i-2)
print(ans)

Solution (Python):


import array
import math
import os
import sys


def main():
    N = read_int()
    print(solve(N))


def solve(N):
    best = N - 1
    x = 2
    while x * x <= N:
        if N % x == 0:
            y = N // x
            best = min(best, x + y - 2)
        x += 1
    return best





DEBUG = 'DEBUG' in os.environ


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


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == '__main__':
    main()

Solution (Python):
import math

N = int(input())

def check(N):
	n2 = math.floor(math.sqrt(N))
	min = N - 1
	for i in range(2, n2 + 1):
		if N % i == 0:
			j = N // i
			d = i + j - 2
			if d < min:
				min = d
	return min

print(check(N))

Solution (Python):
import math
import itertools

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

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

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

a, b = 0, 0

M = N ** (1 / 2) + 1
M = int(M)

for i in range(1, M + 1):

    if N % i == 0:
        a, b = i, N // i


print(a + b - 2)

Solution (Python):
from math import sqrt
N = int(input())
ans = float("inf")
for k in range(1,int(sqrt(N)+1)+1):
    if N%k == 0:
        ans = min(ans,k-1+N//k-1)
print(ans)

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

rN = int(pow(N,1/2) + 1)

res = 1
for i in range(1,rN):
    if N % i == 0:
        res = i

ans = res + N // res - 2

print(ans)

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

def yakusuu(N):
    ret1 = []
    ret2 = []
    for i in range(1,int(N**0.5)+1):
        if N%i==0:
            ret1.append(i)
            ret2.append(N//i)
    return ret1,ret2

X,Y = yakusuu(N)

ans = 10**13
for i,x in enumerate(X):
    y = Y[i]
    ans = min(ans,abs(x-1)+abs(y-1))
print(ans)

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

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

    divisors.sort()
    return divisors

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

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

Solution (Python):
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]





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

print(ans)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
tmp=pow(n,0.5)
tmp=int(tmp)
cnt1=0
cnt2=0
for i in range(tmp):
    if n%(tmp-i)==0:
        cnt1=n//(tmp-i)
        cnt2=tmp-i
        break
print(cnt1+cnt2-2)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from math import sqrt

n = ni()
ans = n-1

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

print(ans)