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 in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)



import java.text.DecimalFormat;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        
        final long N = Long.parseLong(sc.next());
        long ans = 0;
        long min = 1000000000;
        long sqrt = (long)Math.sqrt(N);
        
        if (isPrimeNumber(N)) {
            ans = N - 1;
        } else if (sqrt * sqrt == N) {
            ans = sqrt * 2 - 2;
        } else {
            for (int i = 2; i < N; i++) {
                if (N % i == 0) {
                    long tmp = N / i + i;
                    if (tmp == min) break;
                    min = Math.min(tmp, min);
                }
            }
            ans = min - 2;
        }
        
        out.println(ans);
        
        out.flush();
    }
    
    public static long gcd(long m, long n) {
        long rem;
        while ((rem = m % n) != 0) {
            m = n;
            n = rem;
        }
        return n;
    }
        
    public static long nPr(int n, int r) {
        return factorial(n) / factorial(n - r);
    }
    
    public static long factorial(int n) {
        long result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    
    public static long lucasNumber(int n, long[] memo) {
        memo[0] = 2;
        memo[1] = 1;
        if (memo[n] != 0) return memo[n];
        return memo[n] = lucasNumber(n - 1, memo) + lucasNumber(n - 2, memo);
    }
    
    public static boolean nextPermutation(int[] a) {
        for (int i = a.length - 1; 0 < i; i--) {
            if (a[i - 1] < a[i]) {
                int index = find(a[i - 1], a, i, a.length - 1);
                int temp = a[index];    a[index] = a[i - 1];   a[i - 1] = temp;
                Arrays.sort(a, i, a.length);
                return true;
            }
        }
        return false;
    }

    // destより大きい要素の中で最小のもののインデックスを二分探索で探す
    private static int find(int dest, int[] a, int s, int e) {
        if (s == e) {
            return s;
        }
        int m = (s + e + 1) / 2;
        return a[m] <= dest ? find(dest, a, s, m - 1) : find(dest, a, m, e);
    }

    
    static boolean isPrimeNumber(int targetNum) {
        boolean result = true;
        if (targetNum < 2) result = false;
        int sqrt = (int)Math.sqrt(targetNum);
        for (int i = 2; i <= sqrt; i++) {
            if (targetNum % i == 0) {
                result = false;
                break;
            }
        }
        return result;
    }
    
    static boolean isPrimeNumber(long targetNum) {
        boolean result = true;
        if (targetNum < 2) result = false;
        long sqrt = (long) Math.sqrt(targetNum);
        for (long i = 2; i <= sqrt; i++) {
            if (targetNum % i == 0) {
                result = false;
                break;
            }
        }
        return result;
    }

}