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: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Numerics;
using System.Text.RegularExpressions;
//byte 255 int<2147483647 ≒ 2 * 10^9 long < 9223372036854775807 ≒ 9 * 10^18
//decimal 29桁

class Myon
{
    //chokudaiさんのコードをコピペ
    static Scanner cin;
    public Myon() { }
    public static int Main()
    {
        //Console.SetOut(new Printer(Console.OpenStandardOutput()));
        cin = new Scanner();
        new Myon().calc();
        //test();

        return 0;
    }
    
    public void c_OLD()
    {
        long N = cin.nextLong();
        long rt = (long)Math.Floor( Math.Sqrt(N));
        long temp = 1;

        for(int i = 2; i <= rt; i++)
        {
            while (N % i == 0)
            {
                N /= i;
                temp *= i;
                if (N <= rt) goto endphase;
            }
            
        }
        endphase:
        long ans = (N - 1) + (temp - 1);
        ShowL(ans);
    }
    public void calc()
    {
        long N = cin.nextLong();
        long N2 = N;
        int rt =(int) Math.Floor(Math.Sqrt(N));
        List<int> insuu = new List<int>();
        for (int i = 2; i <= rt; i++)
        {
            while (N2 % i == 0)
            {
                N2 /= i;
                insuu.Add(i);
            }
        }

        long max = tansaku(insuu, rt);
        long remainder = N / max;
        
        ShowL(max + remainder - 2);

    }

    public long tansaku(List<int> l,int max)
    {
        long imax = 2 ^ (l.Count - 1);
        long tempmax = 1;
        long temp = 1;
        for(long i = 0;i< imax; i++)
        {
            temp = 1;
            for(int j = 0; j < l.Count; j++)
            {
                if (i > j & 1 == 1) temp *= l[j];
            }
            if (temp <= max && temp > tempmax) tempmax = temp;

        }

        return tempmax;
    }


    public void d()
    {
        decimal a = (decimal) cin.nextInt();
        decimal b = (decimal) cin.nextInt();
        decimal x = (decimal) cin.nextInt();

        decimal volCond = a/2 * (a * b);
        double ans = -1;
        if( volCond <= x)
        {
            //台形
            
            decimal y =(2 * x / a / a - b);
            
            double z = (double)( a / (b - y));
            ans = 90 - Math.Atan(z) *180/ Math.PI;
            ShowL(ans);
        }
        else
        {
            decimal y2 = a - (decimal) Sqrt((x / b));
            if (y2 < 0 || y2 > a) throw new Exception();
            double z =(double) ((a - y2) / b);
            ans = 90 - Math.Atan(z) * 180 / Math.PI;
            ShowL(ans);
        }

    }
    public static decimal Sqrt(decimal x, decimal epsilon = 0.0M)
    {
        if (x < 0) throw new OverflowException("Cannot calculate square root from a negative number");

        decimal current = (decimal)Math.Sqrt((double)x), previous;
        do
        {
            previous = current;
            if (previous == 0.0M) return 0;
            current = (previous + x / previous) / 2;
        }
        while (Math.Abs(previous - current) > epsilon);
        return current;
    }
    static long nibuntansaku(long[] a, long searchingNumber, long iLast)
    {
        //sが挿入されてソートされるaのindex
        long f = 0, l = iLast;
        long len = a.Length;
        long index = -1;

        while(l - f >= 1)
        {

            //
            index = (f + l) / 2;
            if (a[index] == searchingNumber) { f = index; break; }
            else if (a[index] > searchingNumber) l = index;
            else f = index+1;
        }

        return f;
    }


    #region utilities
    public static void ShowL() { Console.WriteLine(""); }
    public static void ShowL<T>(T s) { Console.WriteLine(s); }
    public static void Show<T>(T s) { Console.Write(s); }
    public static void Yorn(bool isOK,string ifOK = "Yes",string ifNOTOK ="No") { if (isOK) ShowL(ifOK); else ShowL(ifNOTOK); }

    #endregion utilities
}

class Printer : StreamWriter
{
    public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
    public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
    public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { }
}
class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        while (i >= s.Length)
        {
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
        }
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public int[] nextIntArr(long n)
    {
        int[] output = new int[n];
        for (long i = 0; i < n; i++) output[i] = nextInt();
        return output;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] nextLongArr(long n)
    {
        long[] output = new long[n];
        for (long i = 0; i < n; i++) output[i] = nextLong();
        return output;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}
static class SMath
{
    
    /// <summary>
    /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。
    /// 12 なら 2,2,3 です。
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    public static List<long> Factorization(long n)
    {
        if (n <= 0) return null;
        List<long> output = new List<long>();
        int i = 2;
        long max = (long)Math.Sqrt(n); ;
        while (n > 1)
        {
            if (i > max) { output.Add(n); break; }
            while (n % i == 0)
            {
                output.Add(i);
                n /= i;
            }
            i++;
        }
        return output;
    }

    /// <summary>
    /// 素因数分解して、n番目に見つかった素因数をoutput[n-1]に返します。
    /// 84 = (2^2 * 3 * 7) なら 2, 1, 0, 1 　remainder = 1です。(打ち切られない）
    /// 13 = 0,0,...0 remainder = 2019
    /// ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる)
    /// </summary>
    /// <param name="n"></param>
    /// <param name="remainder"></param>
    /// <returns></returns>
    public static List<long> Factorization(long n, out long remainder)
    {
        //output[i] が i-1番目の素数の数を示す
        //ただし、試行を打ち切った時に限り残りをremainderに返す。(remainderは素数または1となる)
        remainder = 1;
        if (n <= 0) return null;
        List<long> output = new List<long>();
        long tempn = 2;
        long max = (long)Math.Sqrt(n);
        bool[] isNOTPrime = new bool[max+1];
        while (n > 1)
        {

            if (tempn > max) { remainder = n; break; }
            if (!isNOTPrime[tempn])
            {
                output.Add(0);
                while (n % tempn == 0)
                {
                    output[output.Count - 1]++;
                    n /= tempn;
                }
                for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true;
            }
            tempn++;
        }
        return output;
    }

    /// <summary>
    /// i番目の素数をoutput[i-1]に返します。
    /// ただしnより大きい素数については判定しません。
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    public static List<long> GetPrimeNumbers(long n)
    {
        if (n <= 0) return null;
        List<long> output = new List<long>();
        long tempn = 2;
        bool[] isNOTPrime = new bool[n + 1];
        while (n > 1)
        {
            if (tempn > n) { break; }
            if (!isNOTPrime[tempn])
            {
                output.Add(tempn);
                for (long j = tempn * 2; j < isNOTPrime.Length; j += tempn) isNOTPrime[j] = true;
            }
            tempn++;
        }
        return output;
    }


    private static long[][] combMemo;
    private static int maxn_combination = -1;
    public static long Combination(int n, int k)
    {
        if (n < 0 || k > n) return 0;
        if (n == k || k == 0) return 1;
        if (k > n/2) return Combination(n, n - k);
        if (maxn_combination < n)
        {
            maxn_combination = n;
            combMemo = new long[n+1][];
        }
        if (combMemo[n] == null) combMemo[n] = new long[n/2 + 1];
        if (combMemo[n][k] != 0) return combMemo[n][k];
        else
        {
            combMemo[n][k] = Combination(n - 1, k) + Combination(n - 1, k - 1);
            return combMemo[n][k];
        }
        
    }
    public static long Gcd(long a, long b)
    {
        if (a <= 0) return -1;
        //if (a > b) return gcd(b, a);
        if (b == 0) return a;
        return Gcd(b, a % b);
    }
    public static long Lcm(long a,long b)
    {
        return a / Gcd(a, b) * b;
    }


    public class pairComparer : IComparer<int[]>
    {
        int index = 0;
        public pairComparer(int n)
        {
            index = n;
        }
        public pairComparer()
        {
            index = 0;
        }
        public int Compare(int[] x, int[] y)
        {
            return y[index] - y[index];
        }
    }

}