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.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC144.C();
        }
        class ABC144
        {
            public static void A()
            {
                var A = Read.Int();
                var B = Read.Int();
                Console.WriteLine(A < 10 && B < 10 ? A * B : -1);
            }
            public static void B()
            {
                var N = Read.Int();
                for (int i = 1; i <= 9; ++i)
                {
                    if (N % i == 0 && N / i <= 9) { Console.WriteLine("Yes"); return; }
                }
                Console.WriteLine("No");
            }
            public static void C()
            {
                var N = Read.Long();
                Console.WriteLine(Util.Divisor(N).Select(d => d + N / d - 2).Min());
            }
            public static void D()
            {
                double A = Read.Int();
                double B = Read.Int();
                double X = Read.Int();
                var rad = X > A * A * B / 2 ? Math.Atan(2 / A * (B - X / A / A)) : Math.Atan(A * B * B / 2 / X);
                Console.WriteLine(rad * 180  / Math.PI);
            }
            public static void E()
            {
                var N = Read.Int();
                var K = Read.Long();
                var A = Read.LongN();
                var F = Read.LongN();
                A = A.OrderBy(x => x).ToArray();
                F = F.OrderBy(x => -x).ToArray();

                Func<long, long> needK = goal =>
                {
                    long total = 0;
                    for(int i = 0; i < N; ++i)
                    {
                        total += Math.Max(0, A[i] - goal / F[i]);
                    }
                    return total;
                };

                long min = -1;
                long max = (long)2e12;
                while (min + 1 < max)
                {
                    var mid = (min + max) / 2;
                    if (needK(mid) > K)
                    {
                        min = mid;
                    }
                    else
                    {
                        max = mid;
                    }
                }
                Console.WriteLine(max);
            }
            public static void F()
            {
                var N = Read.Int();
                var M = Read.Int();
                var Adj = Read.AdjacencyList(N, M, true);
                var dp0 = new double[N + 1];
                for(int i = N - 1; i >= 1; --i)
                {
                    var A = Adj[i];
                    double v = 0;
                    foreach(var a in A)
                    {
                        v += dp0[a];
                    }
                    dp0[i] = v / A.Count + 1;
                }
                double min = dp0[1];
                for(int n = 1; n < N; ++n)
                {
                    var dp1 = new double[N + 1];
                    for (int i = N - 1; i >= 1; --i)
                    {
                        if (i > n)
                        {
                            dp1[i] = dp0[i];
                        }
                        else if (i == n)
                        {
                            var A = Adj[i];
                            if (A.Count == 1)
                            {
                                dp1[i] = dp1[A.ElementAt(0)] + 1;
                            }
                            else
                            {
                                double v = 0;
                                double max = 0;
                                foreach (var a in A)
                                {
                                    v += dp0[a];
                                    max = Math.Max(max, dp0[a]);
                                }
                                dp1[i] = (v - max) / (A.Count - 1) + 1;
                            }
                        }
                        else
                        {
                            var A = Adj[i];
                            double v = 0;
                            foreach (var a in A)
                            {
                                v += dp1[a];
                            }
                            dp1[i] = v / A.Count + 1;
                        }
                    }
                    min = Math.Min(min, dp1[1]);
                }
                Console.WriteLine(min);
            }
        }


        class Util
        {
            public static HashSet<long> Divisor(long x)
            {
                var div = new HashSet<long>(); for (long p = 1; p * p <= x; ++p) { if (x % p == 0) { div.Add(p); } }
                foreach (var d in div.ToArray())
                {
                    if (!div.Contains(x / d)) { div.Add(x / d); }
                }
                return div;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN(); dict[ab[0]].Add(ab[1]);
                    if (!isDirectional) { dict[ab[1]].Add(ab[0]); }
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}