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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
namespace tst
{
    class Program
    {
        public static long min = Int64.MaxValue;
        public static long[] Kumi = new long[2];
        public static List<long> Prime(long x, List<long>L)
        {//素因数分解
            long i = 2;
            long tmp = x;
            while (i * i <= x)
            {
                if (tmp % i == 0)
                {
                    tmp /= i;
                    L.Add(i);
                }
                else
                {
                    i++;
                }
            }
            if (tmp != 1) L.Add(tmp);
            return L;
        }
        public static void Comb(long x, long y, List<long>L, int index)
        {//一番和の小さくなる組を見つける
            if (L.Count == index)
            {
                if (Math.Abs(x - y) < min)
                {
                    min = Math.Abs(x - y);
                    Kumi[0] = x;
                    Kumi[1] = y;
                }
                return ;
            }
            else
            {
                Comb(x * L[index], y, L, index + 1);
                Comb(x , y * L[index], L, index + 1);
            }
        }
        public static void Main()
        {
            long n = long.Parse(Console.ReadLine());
            List<long> L = new List<long>();
            L = Prime(n, L);
            if (L.Count == 1)
            {//素数だったら素因数に１を追加
                L.Insert(0,1);
            }
            //要素がすべて同じ数だったら特別な処理
            bool flag = true;
            long chk = L[0];
            for(int i=0; i<L.Count; i++)
            {
                if (chk != L[i]) flag = false;
            }
            if (!flag)
            {
                Comb(1, 1, L, 0);
            }
            else
            {
                if (L.Count % 2 == 0)
                {
                    Kumi[0] = (long)Math.Pow(L[0],  L.Count / 2);
                    Kumi[1] = Kumi[0];
                }
                else
                {
                    double val = L.Count / 2;
                    Kumi[0] = (long)Math.Pow(L[0],(int)Math.Floor(val));
                    Kumi[1] = Kumi[0]*L[0];
                }
            }
            Console.WriteLine(Kumi[0] + Kumi[1] - 2);
        }
    }
}