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.



Sample input:
10


Sample output:
5

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline

def main():
    N = int(input())
    if N == 1:
        print(0)
        quit()

    ans = float('INF')
    m = math.ceil(N ** 0.5)
    for a in range(1, m + 1):
        if N % a == 0:
            b = N // a
            ans = min(ans, (a + b - 2))

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

i = 1
j = 1 
l = []
while i <= (N/i):
  if N%i == 0:
    j = int(N/i)
    l.append([i,j])
  
  i+=1

res = l.pop()
print(res[0]+ res[1] -2)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    xdiv = int(n**0.5)
    for i in range(xdiv, 0, -1):
        if n % i ==0:
            dest = i + n//i
            print(dest-2)
            exit()
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
sqrtN = math.sqrt(N)
floorN = math.floor(sqrtN)
for x in range(floorN, 0, -1):
    if N % x == 0:
        y = N // x
        print(x + y - 2)
        break

Submission status: Accepted

Solution code:
#144_C
n = int(input())
x = 1
for k in range(2, int(n**0.5)+1):
    if n % k == 0:
        x = k
print(x + n // x - 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
import math
import sys

n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())
dv = float('inf')
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

div = make_divisors(n)
# print(make_divisors(n), len(make_divisors(n)))
# print(make_divisors(n)[169//2])
l = len(make_divisors(n))
# print(div)
if l % 2 == 0:
    for i in range(l//2):
        if dv > (div[i]-1) + (div[l-i-1]-1):
            dv = (div[i]-1) + (div[l-i-1]-1)
else:
    for i in range((l//2)+1):
        if dv > (div[i]-1) + (div[l-i-1]-1):
            dv = (div[i]-1) + (div[l-i-1]-1)

print(dv)

Submission status: Accepted

Solution code:
import math

N = int(input())
min_ans = N

for a in range(1, int(N ** 0.5) + 1):
    if N % a != 0:
        continue
    b = N // a
    min_ans = min(min_ans, a + b - 2)

print(int(min_ans))

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**12
for i in range(1,10**6+1):
  j = N//i
  if i*j == N:
    ans = min(i+j-2,ans)
  if i == 10**6 and ans == 10**12:
    ans = N-1
    break
print(ans)

Submission status: Accepted

Solution code:
x=int(input())
ans=10**13
for i in range(1,round(x**0.5)+2):
	if x%i==0 and x//i<=ans:
		ans=x//i
print(ans+x//ans-2)

Submission status: Accepted

Solution code:
n=int(input())
i=0
s=[]
p=0
for i in range(int(n**0.5)):
    i+=1
    if n%i==0:
        p=1
        s.append(n//i+i-2)
print(min(s))

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
N = int(input())
N_2 = int(N ** 0.5)
ans = float('Inf')
for i in range(1, N_2 + 1):
    if N % i == 0:
        s = i + N / i - 2
        ans = min(ans, int(s))
print(ans)

Submission status: Accepted

Solution code:


import math


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

    min_manhattan = float('inf')
    for x in range(1, math.ceil(math.sqrt(n)) + 1):
        q, mod = divmod(n, x)
        if mod:
            continue

        if x + q < min_manhattan:
            min_manhattan = x + q

    print(min_manhattan - 2)


if __name__ == "__main__":
    submit()

Submission status: Accepted

Solution code:
import math
n=int(input())
p=math.sqrt(n)
def x(g):
  a=g
  c=0
  while a>=0:
    a-=1
    c+=1
  return c-1

m=x(p)
for i in range(m):
  if n%(m-i)==0:
    print(m-2-i+(n//(m-i)))
    break

Submission status: Accepted

Solution code:
import math
def is_prime(n):
    for i in reversed(range(1,math.ceil(n**0.5)+1)):
        if n%i == 0:
            print(n//i+i-2)
            exit()
    print(n-1)

N = int(input())
is_prime(N)

Submission status: Accepted

Solution code:
from math import sqrt

n = int(input())
now = sqrt(n) // 1
while n / now % 1 != 0:
  now -= 1

print(int(now + (n // now) -2))

Submission status: Accepted

Solution code:
import math
N = int(input())
goal = [0,0]
ans = 10**12
for i in range(1,int(math.sqrt(N))+1):
    if(N%i == 0):
        goal = [i,N//i]
        distance = goal[0]+goal[1]-2
        ans = min(ans,distance)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
N2 = N ** (1 / 2)
ans = float('Inf')
for i in range(1, int(N2) + 1):
    if N % i == 0:
        a = i + N // i - 2
        if ans > a:
            ans = a
print(ans)

Submission status: Accepted

Solution code:
def factorize(n):
    b = 2
    fct = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            fct.append(b)
        b = b + 1
    if n > 1:
        fct.append(n)
    return fct

def solve():
    N = int(input())
    l = factorize(N)
    ans = 10 ** 12
    for i in range(1, int(N ** 0.5)+1):
        if N % i == 0:
            a = N // i
            ans = min(ans, a+i)
    return ans-2        
print(solve())

Submission status: Accepted

Solution code:
#abc144 c
import math
n=int(input())
x=int(math.sqrt(n))
ans=n+1
for i in range(1,x+1):
    if n%i==0:
        move=i+n//i-2
        if move<ans:
            ans=move
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
div = []
ans = N - 1
i = 2
while i ** 2 <= N:
    if N % i == 0:
        div.append(i)
    i += 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
N2 = N ** (1 / 2)
ans = float("inf")
for i in range(1, int(N2) + 1):
    if N % i == 0:
        a = i + (N // i) - 2
        if ans > a:
            ans = a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x=int(input())
ans=10**13
for i in range(1,round((x**0.5))+2):
	if x%i==0:
		if ans>(i-2+(x//i)):
			ans=(i-2+(x//i))
print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
list=[]#素因数の格納
for i in range(1,int(math.sqrt(N))+2):
    if N//i==N/i:
        list.append(N//i+i)
print(min(list)-2)

Submission status: Accepted

Solution code:
n=int(input())
x,y=(1,1)
m=10**12+1
for i in range(1, int(n**0.5)+1):
    x+=1
    if n%x==0: y=n//x
    if n==x*y and m>x+y-2:
        m=x+y-2

if m==10**12+1:
    m=n-1
print(m)

Submission status: Accepted

Solution code:
N = int(input())
i = int(N**0.5)
while N%i!=0:
  i -= 1
print(i+N//i-2)

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = 10**12
for i in range(1,math.floor(math.sqrt(n))+1) :
  if n%i != 0 :
    continue
  ans = min(ans,i+n//i-2)

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
print(min([a + N // a - 2 for a in range(math.floor(math.sqrt(N)) + 1, 0, -1) if N % a == 0]))

Submission status: Accepted

Solution code:
import math

N = int(input())

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

Submission status: Accepted

Solution code:
from math import sqrt, floor
n = int(input())
for i in range(floor(sqrt(n)), 0, -1):
    if n % i == 0:
        j = n // i
        print(i+j-2)
        exit()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 10 21:05:26 2020

@author: Kanaru Sato
"""

import math

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

list = []

for i in range(1, rootn+1):
    if n%i ==0:
        list.append([i,n//i])

dist = list[-1][0]+list[-1][1]-2

print(dist)

Submission status: Accepted

Solution code:
import math
N = int(input())
ans = 10**12
for i in range(1,10**6+1):
  j = math.ceil(N/(i+1))
  if (1+i)*j == N:
    ans = min(i+j-1,ans)
  if i == 10**6 and ans == 10**12:
    ans = N-1
    break
print(ans)

Submission status: Accepted

Solution code:
import math

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

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

print(j - 1 + i - 1)

Submission status: Accepted

Solution code:
a=int(input())
k=int(a**0.5)
while a%k:k-=1
print(k+(a//k)-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

lis = make_divisors(n)

a = lis.pop()
b = n//a
ans = a+b-2

print(ans)

Submission status: Accepted

Solution code:
import math

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

    minimum = 10**12
    for i in range(1,math.floor(N**0.5) + 1):
        if(N % i == 0):
            x = i
            y = N // i

            if(minimum > x+y):
                minimum = x + y

    print(minimum - 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

min = n + 1
for i in range(1,int(math.sqrt(n))+5):
    if n%i==0:
        min = i + (n // i) if min > i + (n // i) else min

print(min - 2)

Submission status: Accepted

Solution code:
N=int(input())
n=int(N**0.5)
l=[]
for i in range(1,n+1):
  ll=[]
  if N%i==0:
    ll.append(i)
    ll.append(int(N/i))
    l.append(ll)
mins=10**12
for j in range(len(l)):
  s=l[j][0]-1+l[j][1]-1
  if s<mins:
    mins=s
print(mins)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

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

Submission status: Accepted

Solution code:
N = int(input())
i = 2
ans = N - 1

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

print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
N = int(input())
ans = 10**18
for i in range(1, int(sqrt(N))+1):
    if N//i==i and N%i==0:
        ans = (i-1)*2
        break
    else:
        if N%i==0:
            #print((N//i)-1, i-1)
            tmp = ((N//i)-1)+(i-1)
            ans = min(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:
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())

divisors = make_divisors(n)
print(min([sum(x)-2 for x in divisors]))

Submission status: Accepted

Solution code:
#C
N = int(input())

inf = float("inf")
MIN = inf

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

print(MIN)

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
import collections
import itertools
sys.setrecursionlimit(100001)

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

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

ans = sys.maxsize
for i in range(1, n, 1):
    if i > math.sqrt(n):
        break

    if n % i ==0:
        ans = min(ans, n//i -1 + i - 1)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(2,int((n**0.5))+1):
  if n%i==0:
    l.append(i)
if len(l)>=1:
  print(max(l)-1+n//max(l)-1)
else:
  print(n-1)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    n_root = int(n**(1/2)) + 100

    c = 10 ** 18

    for i in range(1,n_root):
        if n % i == 0:
            c_new = (n // i) + i
            c = min(c_new,c)
    
    print(c - 2)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math as m
n = int(input())
max1 = 0
sqr = m.ceil(m.sqrt(n) + 1)
if n == 1:
	print(0)
	exit()
for i in range(1,sqr):
	if n%i == 0:
		max1 = max(max1, i)
print(max1 + n//max1 - 2)

Submission status: Accepted

Solution code:
import math

n = int(input())
tmp_list = []

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

i = tmp_list[len(tmp_list)-1]
j = n//i

print(i+j-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())

# 証明できないけど、真ん中の方の数字の方が数を足すと小さい値になると思う。
for n in reversed(range(1, int(math.sqrt(N)) + 1)):
  if N % n == 0:
    a = int(n + N/n)
    break


print(a - 2)

Submission status: Accepted

Solution code:
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 - 2)

print(ans)

Submission status: Accepted

Solution code:

n = int(input())
number = int(n ** 0.5)+1
ans = 1000000000000

for i in range(1,number):
    if n % i == 0:
        tmp = i + int(n / i) -2
        if tmp < ans:
            ans = tmp

print(ans)

Submission status: Accepted

Solution code:
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

ans=10**12
for i in make_divisors(N):
    j=N//i
    if ans>i+j:
        ans=i+j

print(ans-2)

Submission status: Accepted

Solution code:
n = int(input())

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

print(min(t) - 2)

Submission status: Accepted

Solution code:
N = int(input())

Min = 2*(10**12)-2
i=1

while(i<=(N**0.5)):
    j = N/i
    if j.is_integer():
        if (i+j-2)<Min:
            Min=i+j-2
    i+=1
    
print(int(Min))

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


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(stdin.readline().rstrip())

d = make_divisors(n)
m = len(d)//2

start = [1, 1]
goal = []

if len(d) % 2 == 0:
    goal = [d[m-1], d[m]]
else:
    goal = [d[m], d[m]]

ans = goal[0] + goal[1] - 2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

U = np.arange(1, n ** 0.5 + 1, dtype = np.int64) # +1を忘れない！

div = U[n % U == 0]
ans = min((n / div) + div) - 2

print(int(ans))

"""old


from itertools import combinations
import numpy as np

n = int(input())

a = 2
cnt = 0
ans = []


def walk_counter(ll, n):
    left = np.prod(ll)
    right = n // left
    return left + right - 2


tmp_n = n

while tmp_n >= a:
    while tmp_n % a == 0:
        tmp_n = tmp_n // a
        # cnt += 1
        ans.append(a)
    # if cnt > 0:
    #     ans.append([a, cnt])
    #     cnt = 0
    a = a + 1

if tmp_n == a:
    ans.append(a)

fin = 0

for i in range(len(ans)):
    for v in combinations(ans, i):
        tmp = walk_counter(v, n)
        if i == 0:
            fin = tmp
        elif fin > tmp:
            fin = tmp

# 組み合わせでなく、√nまでの試し割法のほうが早い

print(fin)
"""

Submission status: Accepted

Solution code:
n = int(input())
a = 1
for i in range(1, int(n**(1/2)+2)):
	if n%i==0 : a = max(a, i)

print(int(a + n/a -2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import numpy as np

N = int(input())
root_N = int(math.floor(math.sqrt(N))+1)

res = []
for a in range(1, root_N):
  if (N/a).is_integer():
    res.append(a + N/a - 2)

res = np.array(res)
print(int(res.min()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

L = divisors(N)
ans = float('inf')

for l in L:
    tmp = sum(l) - 2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    for i in range(int(n**.5)+1,0,-1):
        if  not n%i:
            m = n//i
            print(i+m-2)
            return
main()

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = 100100100100100
a = 1
while a <= math.sqrt(n):
    if n%a == 0:
        ans = min(ans, int(a + n/a - 2))
    a += 1

print(ans)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=inf
  for i in range(1,int(math.sqrt(n))+1):
    if n%i==0:
      ans=min(ans,i+n//i-2)
  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
import os
import math

def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())

    max = int(math.sqrt(N) + 1)
    ret = float('inf')
    for i in range(1, max):
        q, mod = divmod(N, i)
        if mod == 0:
            j = N // i
            ret = min(ret, ((j-1)+(i-1)))

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())

    ret = float('inf')
    for i in range(1, 10000000):
        q, mod = divmod(N, i)
        if mod == 0:
            j = N // i
            ret = min(ret, ((j-1)+(i-1)))

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
#n=a*bのa+b-2のmin
ans=float("inf")
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    ans=min(ans,n//i+i-2)
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
sn = sqrt(n)


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

Submission status: Accepted

Solution code:
import math
#from collections import deque

n = int(input())
# if n == 1:
#     print("0")
#     exit()

# flag = True
# for k in range(2, int(math.sqrt(n)) + 1):
#     if n % k == 0:
#         flag = False
#
# if flag:
#     print("素数！！")
#     print(n - 1)
#     exit()

# s = [[1,2,1]]
# s = [[1,2]]
# q = deque(s)
#
# count = 0
# num = 0
# while True:
#     ans = q.popleft()
#     # if (ans[0]+1)*(ans[1]) == n or (ans[0])*(ans[1]+1) == n:
#     if (ans[0]+1)*(ans[1]) == n or (ans[0])*(ans[1]+1) == n:
#         # print(ans[2] + 1)
#         print(ans[0] + ans[1] - 1)
#         exit()
#
#     else:
#         if ans[0] == ans[1]:
#             if (ans[0]+1)*(ans[1]) == n or (ans[0])*(ans[1]+1) == n:
#                 print(ans[0] + ans[1] - 1)
#                 exit()
#
#         else:
#             if ([ans[0]+1, ans[1]]) not in q:
#                 q.append([ans[0]+1, ans[1]])
#             if ([ans[0], ans[1]+1]) not in q:
#                 q.append([ans[0], ans[1]+1])
ansMax = 1000000000000
num = int(math.sqrt(n))
for i in range(1, num + 1):
    if n % i == 0:
        b = (n // i)
        if i + b - 2 < ansMax:
            ansMax = i + b - 2

print(ansMax)

Submission status: Accepted

Solution code:
import math
n=int(input())
if n==4:
    print(2)
elif n<=5:
    print(n-1)
else:
    ans=n-1
    for i in range(math.floor(n**0.5),1,-1):
        if n%i==0:
            ans=i+n//i-2
            break
    print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

start = int(math.sqrt(n)+0.5)
l =[]
for i in range(1,start+1):
    if n % i == 0:
        a = i
        b = n // i
        l.append(a+b-2)

print(min(l))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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())
ls = make_divisors(n)
pairs = []
for i in ls:
    pair = (i, n//i)
    pairs.append(pair)
count = []
for pair in pairs:
    count.append(pair[0] + pair[1] - 2)
print(min(count))

Submission status: Accepted

Solution code:
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-


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


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

    div = divisor(N)

    result = []
    for d in div:
        result.append(N // d + d - 2)
    print(min(result))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

num = int(input())
limit = int(math.sqrt(num))

min = num

for i in range(1,limit+1):
  sign = num%i
  if sign == 0:
    temp = (num//i) + i - 2
    if min > temp:
      min = temp 
      
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

    # divisors.sort()
    return divisors

list_n = make_divisors(n)

a = len(list_n)
ans = []
for i in range(0, a-1, 2):
    ans.append(list_n[i]+list_n[i+1])

ans.sort()

print(ans[0]-2)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    ans = inf
    for i in range(1,int(sqrt(N)+1)):
        if N%i==0:
            ans = i+N//i-2

    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import fractions


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    m = 1
    for i in range(2, int(n ** 0.5) + 1):
        m = max(m, fractions.gcd(n, i))
    print(n // m + m - 2)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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-2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

ans = 10 ** 18

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

print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

ans = 10**13


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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

tmp = 10 ** 24
tmp2 = 1
tmp3 = 1
for i in range(1, 10 ** 6 + 1):
    if n % i == 0 and i + (n / i) < tmp:
        tmp = i + (n // i)
        tmp2 = i
        tmp3 = n // i
print(tmp2 + tmp3 - 2)

Submission status: Accepted

Solution code:
import sys

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)
#print(a)
ans = sys.maxsize
for x in a:
    y = n // x
    #print(x, y, x+y)
    ans = min(ans, x + y - 2)
print(ans)

Submission status: Accepted

Solution code:
import sys
def solve():
    N = int(input())
    moveN = 10000000000000000000
    for i in range(1, N + 1):
        if i ** 2 > N: break
        if N % i == 0:
            moveN = min(i + (N // i) - 2, moveN)
    print(moveN)

    return 0

if __name__ == "__main__":
    solve()