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:
from math import sqrt
from math import floor
n = int(input())
for i in range(floor(sqrt(n)), 1, -1):
    if n % i == 0:
        min_div = i
        break
else:
    min_div = n
ans = min_div + int(n / min_div) - 2
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)
    return divisors


list = make_divisors(n)
min = 1e13
for i in list:
    if n//i+i < min:
        min = n//i+i

print(min-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

divs = make_divisors(N)
m_div = divs[(len(divs)+1)//2-1]
M_div = divs[-((len(divs)+1)//2)]

print(m_div+M_div-2)

Submission status: Accepted

Solution code:
import math
min_steps = 1e18
number = int(input())
a = math.sqrt(number)
for k in range (1,int(a)+1):
    if number%k == 0:
        i = number//k
        steps = k + i - 2
        if steps <= min_steps:
            min_steps = steps
print(min_steps)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

ans=1000000000000

for i in range(1,n+1):
    if N%i==0:
        if ans>i+(N/i):
            ans=i+(N/i)

print(int(ans-2))

Submission status: Accepted

Solution code:
a = int(input())
s = int(a ** 0.5)
for i in range(s):
  if a%(s-i)==0:
    print(int(a/(s-i) + (s-i) - 2))
    break

Submission status: Accepted

Solution code:
import math
input_num = int(input())
sqrt_num = math.floor(math.sqrt(input_num))

min_count = 1 + input_num
for i in range(sqrt_num + 1, 1, -1):
  if(input_num % i == 0) and (min_count > input_num//i + i):
    min_count = input_num//i + i
print(min_count - 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math
a = int(input())
x = int(math.sqrt(a))
i = 0
while True:
    b = x - i
    if (a % b) == 0:
        print(int(b + (a/b) - 2))
        break
    i += 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
maxi = int(np.sqrt(n))
ans = 10**15
for a in range(1, maxi+1):
    if n % a ==0:
        b = int(n / a)
        if a+b -2 < ans:
            ans = a + b - 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#a,b=map(int,input().split())
n=int(input())
import math
ans=[]
for i in range(1,int(math.sqrt(n))+1):
    if n%i==0:
        dis=abs(i-1)+abs(n//i-1)
        ans.append(dis)
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())

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

x = divisor[-1]
y = N // x

print(x + y - 2)

Submission status: Accepted

Solution code:
import math

n = int(input())

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

print(res)

Submission status: Accepted

Solution code:
import math
def solve():
    N = int(input())
    rootN = math.sqrt(N)

    min_val = float('inf')
    for a in range(1, math.floor(rootN) + 1):
        if N % a == 0:
            b = N / a
            min_val = min(min_val, a + b - 2)
    
    print(int(min_val))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
ans=float("inf")
for i in range(1,10**6+1):
    q,m=divmod(n,i)
    if m==0:
        ans=min(ans,q+i-2)
ans=min(ans,1+n)
print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc144/tasks/abc144_c
import math
N = int(input())
m = math.ceil(math.sqrt(N))
for k in range(m,0,-1):
    if N % k == 0:
        i = k
        break
print(i+N//i-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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())
list = make_divisors(N)

if len(list)%2 == 0:
    a,b = list[-2],list[-1]
    print(a+b-2)
else:
    a = list[-1]
    print(2*a-2)

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


num_lists = make_divisors(N)
ans = 10**12
for num in num_lists:
  x = N//num
  y = num
  ans = min(x-1+y-1,ans)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
import math
m=None
for i in range(1,int(math.sqrt(n))+1):
  if n%i==0:
    #print(i, n//i)
    #print(i-1, n//i-1)
    if m is None or m > (i+n//i-2):
      m=i+n//i-2
    #print()
print(m)

Submission status: Accepted

Solution code:
N = int(input())
lst = []

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
------------------------
author : iiou16
time : 11/01/12:15
------------------------
'''


def main():
    N = int(input())
    org = N
    i = 2
    table = []
    while i * i <= N:
        if N % i == 0:
            j = org / i
            table.append(i + j - 2)
        i += 1
    table.append(org - 1)
    print(int(min(table)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import numpy as np

N = int(input())

ans = 2*N
upper = math.ceil(np.sqrt(N))

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

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
a = math.ceil(math.sqrt(n))
min_ans = float('inf')

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
ans =float("inf")
n=int(input())
m=int(math.sqrt(n))+1
for i in range(m):
    if i==0:
        continue
    if n%i==0:
        ans=min(ans,i-1+(n/i)-1)
print(int(ans))

Submission status: Accepted

Solution code:
import math
import sys

N = int(input())

a = sys.maxsize

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

print(a)

Submission status: Accepted

Solution code:
n = int(input())

#(1,1)から一番長く移動しないといけないのは(1,n)の時
ans = n - 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
i = int(n**0.5)
while True:
    if n%i == 0:
        print(i + n//i - 2)
        break
    i -= 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    r = math.ceil(math.sqrt(n))
    ans = 0

    for i in range(1, r+1)[::-1]:
        if n % i == 0:
            w = i
            h = n / w
            ans = int(w) + int(h) -2
            break

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

a=int(input())

lst=[]
for i in div_make(a):
    lst.append(i[0]+i[1])
ans=0
ans=min(lst)-2
print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
def f(N):
  minn=10000000000000
  k=int(math.sqrt(N)+1)
  for i in range(1,k):
    j=N//i
    if i*j==N :
      minn=min(minn,i-1+j-1)
  return(minn)
print(f(N))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    # 約数のペア(a,b)で、a+bが最小となるものが答え
    ans = float("inf")
    for i in range(1, N):
        if i > N**(1/2):
            break

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
S = []
ans = float('inf')
flag = False
for i in range(2,1000001):
  if n%i == 0:
    S.append([i,n//i])
    flag = True
if flag:
  for i in range(len(S)):
    if S[i][0]+S[i][1]-2 < ans:
      ans = S[i][0]+S[i][1]-2
else:
  ans = n-1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

ress = []

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

print(min(ress)-2)

Submission status: Accepted

Solution code:
N = int(input())
n = round(N ** (1/2))
i = n
m = 0
while m == 0:
    if N%i == 0:
        m = N/i
    else:
        i -=1
print(round(i+m-2))

Submission status: Accepted

Solution code:
a = int(input())
ans = []

for i in range(1,int(a**0.5)+1):
  b = a /i
  if b.is_integer():
    ans.append(int(b+i))

print(min(ans) - 2)

Submission status: Accepted

Solution code:
import math
n = int(input())
ans = float('inf')
for i in range(1,10**6 + 1)[::-1]:
    if n%i == 0:
        ans = min(ans,i + n//i - 2)

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)
            if i != n // i:
                divisors.append(n//i)

    return divisors


n = int(input())
a = make_divisors(n)
b = []

if len(a) % 2 == 0:
    for i in range(0,len(a),2):
        sum = a[i] + a[i+1]
        b.append(sum)
else:
    b.append(a[len(a)-1]*2)
    for i in range(0,len(a)-1,2):
        sum = a[i] + a[i+1]
        b.append(sum)

b.sort()
print(b[0]-2)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

n = int(input())
ans = n - 1

i = 1
while i*i <= n:
    q, m = divmod(n, i)
    if m == 0:
        ans = min(ans, i+q-2)
    i += 1

print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    sys.setrecursionlimit(2000)
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    mt = 10 ** 20
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            mt = min(mt, i + (n // i) - 2)
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def solve(num):
  n = int(num)
  for i in range(int(n**0.5)+1, 0, -1):
    if n%i:
      continue
    else:
      j = n/i
      sum = i+j-2
      return(int(sum))
      
      
if __name__ == "__main__":
  print(solve(input()))

Submission status: Accepted

Solution code:
def solve(string):
    n = int(string)
    for i in range(int(n**0.5), 0, -1):
        if not n % i:
            return str(i + n // i - 2)


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

Submission status: Accepted

Solution code:
def solve(string):
    n = int(string)
    for i in range(int(n**0.5) + 1, 0, -1):
        if not n % i:
            return str(i + n // i - 2)


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

Submission status: Accepted

Solution code:
def solve(string):
    n = int(string)
    for i in range(int(n**0.5) + 1, 0, -1):
        if n % i == 0:
            return str(i + n // i - 2)


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

Submission status: Accepted

Solution code:
def solve(string):
    n = int(string)
    for i in range(int(n**0.5) + 1, 0, -1):
        if n % i:
            continue
        else:
            return str(i + n // i - 2)


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

Submission status: Accepted

Solution code:
import math 

N=int(input())

li=[]
l=0
m=0

for i in range(math.floor(math.sqrt(N))):
  if N%(i+1)==0:
    l=i
    m=N/(i+1)-1
    li.append(l+m)
  else:
    pass

print(int(min(li)))

Submission status: Accepted

Solution code:
def solve(string):
    n = int(string)
    l = int(n**0.5)
    while n % l > 0:
        l -= 1
    return str(l + n // l - 2)


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

Submission status: Accepted

Solution code:
n = int(input())

for i in range(int(n**.5), 0, -1):
    if n%i:
        pass
    else:
        print(i + n//i - 2)
        break

Submission status: Accepted

Solution code:
N = int(input())

for i in range(int(N**.5), 0, -1):
    if(N%i):
        pass
    else:
        print(i + N//i - 2)
        break

Submission status: Accepted

Solution code:
import  math

n = int(input())

ans = n-1

for i in range(1,math.ceil(math.sqrt(n))+1):
    if n % i == 0: ##もしiがnの約数ならば
        j = int(n / i) #i x j = N を j = N / iに式変形
        ans = min(ans,i+j-2) #(1,1)から(i,j)に至るまでの　i-1+j-1 = i + j - 2の最小値を求める

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

i=1
List=[]
while i<n**.5+1:
    if n/i==n//i:
        List.append(n//i)
    i=i+1
print(List[-1]+n//List[-1]-2)

Submission status: Accepted

Solution code:
import math
n = int(input())
m = int(math.sqrt(n)) + 1

ans = n - 1

for a in range(2, m):
  if n % a == 0:
    ans = min(ans, a + n//a - 2)
    
print(ans)

Submission status: Accepted

Solution code:
# coding=utf-8
import math

if __name__ == '__main__':
    N = int(input())
    mem = 1000000000000000000000000000000000000

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

    print(int(mem))

Submission status: Accepted

Solution code:
n=int(input())
i=1
ans=n-1
while i*i <= n:
  if n%i==0:
    ans=i+n/i-2
  i=i+1
ans=int(ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

import math
sq = math.sqrt(n)
a = math.floor(sq)

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

print(n-1)

Submission status: Accepted

Solution code:
from math import sqrt, floor

N = int(input())
n = floor(sqrt(N))

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import math

N = int(input())

sn = int(math.sqrt(N) // 1)
ans = N

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n = int(input())
answer = n
for x in range(1, math.ceil(math.sqrt(n) + 1)):
    if n % x == 0:
        y = n // x
        answer = min(answer, x + y - 2)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


def main():
    a = int(input())
    for i in range(int(math.sqrt(a)) + 1, 0, -1):
        if(a % i == 0):
            print(int(i + (a / i) - 2))
            return


if(__name__ == "__main__"):
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

    INF = 10**6 + 100

    x = np.arange(1,INF,dtype=np.int64)
    div = x[N % x == 0]
    #print(div)
    #print(N // div)
    
    ans = (div + N//div).min() - 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
a=int(input())
a_max=math.ceil(math.sqrt(a))
ans_1=1
for i in range(a_max):
  x=i+1
  if a%x==0 :
    ans_1=x
ans_2=a/ans_1
ans=int(ans_1+ans_2-2)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

ans = n-1

for i in range(1, math.ceil(math.sqrt(n))+1):
  if n % i == 0:
    j = int(n / i)
    ans = min(ans, i + j - 2)
    
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 and i != n // i:
            a, b = i, n//i
            divisors.append((a, b))
        elif n % i==0:
            divisors.append((i, i))

    # divisors.sort()
    return divisors
  
N = int(input())
conv_list = make_divisors(N)
ans_taple = conv_list[-1]
print(sum(ans_taple) -2)
print()

Submission status: Accepted

Solution code:
import sys
 
input = sys.stdin.readline()
N = int(input)
sum = N
if N % 2 == 0:
  halfN = N/2
else:
  halfN = N+1/2
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)
for x in divisors:
  if N%x==0 and (N/x+x-2)<N:
    sum = N/x + x - 2
print(int(sum))

Submission status: Accepted

Solution code:
a=int(input())
for x in range(int(a**0.5),0,-1):
  if a%x==0:
    b=int(a//x)
    break
print(b+x-2)

Submission status: Accepted

Solution code:

def main():
    s = int(input())
    root = int(pow(s,0.5))
    temp = 0
    for i in reversed(range(1,root+1)):
        if s%i == 0:
            temp = i
            break
    ans = temp - 1 + (s//temp) - 1

    print(ans)






if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
f=0
moves=9999999999999999999999999999
for i in range(1,int(n**0.5)+1):
    if n%i==0:
        x=i
        y=n//i
        moves=min(moves,(abs(x-1)+abs(y-1)))
print(moves)

Submission status: Accepted

Solution code:
N = int(input())
n = int(N**0.5)

for i in range(n):
  i = n-i
  if N%i==0:
    print(N//i + 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

l = make_divisors(n)
ans = n
for i in range(len(l)):
  ans = min(ans,(l[i] + n // l[i]) - 2)
print(ans)

Submission status: Accepted

Solution code:
import math
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())


N=II()
res=INTMAX
for i in range(1, math.floor(math.sqrt(N))+1):
    if N % i == 0:
        res = min(res, i + (N // i) - 2)
print(res)

Submission status: Accepted

Solution code:
import math
n=int(input())
ans=n
i=1
while(i<=math.sqrt(n)):
    if n%i==0:
        d=i+n/i-2
        ans=min(ans,d)
    i+=1
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

mi = 10**100

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

print(mi)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math
n = int(input())
m = int(math.sqrt(n))
while n % m != 0:
  m -= 1
print(n//m + m -2)

Submission status: Accepted

Solution code:
from math import floor,sqrt 
num = int(input())

min_divisor=floor(sqrt(num))

for i in range(min_divisor,0,-1):
    if num%i ==0:
        min_step= i +(num/i)-2
        break
    

print(int(min_step))

Submission status: Accepted

Solution code:
import copy
import heapq
import math
import fractions

def j(n):
    if n:print("Yes")
    else:print("No")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printmultiplerow(a):
    for i in a:
        print(i)
def printrow(a):
    for i in range(len(a)-1):
        print(a[i],end = " ")
    print(a[-1])


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

Submission status: Accepted

Solution code:
N = int(input())

ans = None
for a in range(1, int(N**0.5) + 1):
    b = N / a
    if float.is_integer(b):
        dist = a + b - 2
        if ans is None or dist < ans:
            ans = int(dist)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
N2 = int((N ** (1/2)) //1)
#print(N2)

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

m = 10**12
for a in range(1, limit+1):
    b = n // a
    if a*b == n and a+b-2 < m:
        m = a+b-2

print(m)

Submission status: Accepted

Solution code:
N = int(input())
ans = float('inf')
for i in range(1,int(N**0.5) + 1):
    tmp = N / i
    if tmp.is_integer():
        if tmp == N:
            tmp_ans = N - 1
        else:
            tmp_ans = int(tmp) + i - 2
        if ans > tmp_ans:
            ans = tmp_ans
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 10 ** 18
for i in range(int(n ** 0.5) + 1, 0, -1):
  if not n % i:
    ans = i + n // i - 2
    break
print(ans)

Submission status: Accepted

Solution code:
import math

def finding(n):
    nn = math.floor(math.sqrt(n))
    if nn*nn == n:
        return nn, nn
    else:
        for i in range(nn, 0, -1):
            if n % i == 0:
                return i, n//i


n = int(input())
nn1, nn2 = finding(n)
print(nn1+nn2-2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = int(n**0.5)
i = 1
while i <= a:
    if n % i == 0:
        b = i
    i = i + 1
c = int(n / b)
print(b+c-2)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
def solve(n):
    ans = n-1
    i = 1
    while i*i <= n:
        if n % i == 0:
            ans = min(ans, i+n//i-2)
        i += 1
    return ans

n = int(input())
print(solve(n))

Submission status: Accepted

Solution code:
import math
n = int(input())
m = int(math.sqrt(n))
while n % m != 0:
  m -= 1
print(n//m + m -2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math

def main():
    n = int(input())
    res = float('inf')

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

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math
N=int(input())    
NS=math.floor(N**0.5)

ans=1000000000000
for i in range(NS,0,-1):
  if N%i==0:
    tmp=N//i
    anst=tmp+i-2
    ans=min(ans,anst)
            
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    ans = float('inf')

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
import math
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())
min=N-1
parr=make_divisors(N)
for i in range(len(parr)):
    if(parr[i]+N//parr[i]-2<N):
        min=parr[i]+N//parr[i]-2
print(min)

Submission status: Accepted

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