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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

public static class Program
{
    public static void Main(string[] args)
    {
        var n = long.Parse(Console.ReadLine());
        var primeFactor = new PrimeFactor();
        var factors = primeFactor.Enumerate(n).ToList();
        factors.Add(1);
        long minValue = long.MaxValue;
        for(var i = 1; i < factors.Count; i++)
        {
            foreach (var x in Combinations(factors, i))
            {
                var y = new List<long>(factors);
                foreach (var e in x)
                {
                    y.Remove(e);
                }
                minValue = Math.Min(minValue, x.Aggregate((now, next) => now * next) + y.Aggregate((now, next) => now * next));
            }
        }
        Console.WriteLine(minValue - 2);
    }

    private static IEnumerable<List<TSrc>> Combinations<TSrc>(List<TSrc> src, int n)
    {
        return Enumerable.Range(0, n - 1)
            .Aggregate(
                Enumerable.Range(0, src.Count() - n + 1)
                    .Select(num => new List<int>() { num }),
                (list, _) => list.SelectMany(c =>
                    Enumerable.Range(c.Max() + 1, src.Count() - c.Max() - 1)
                        .Select(num => new List<int>(c) { num })
                    )
                )
            .Select(c => c
                .Select(num => src[num])
                .ToList()
                );
    }

    public class PrimeFactor
    {

        public IEnumerable<long> Enumerate(long n)
        {
            while (n > 1)
            {
                long factor = GetFactor(n);
                yield return factor;
                n = n / factor;
            }
        }

        private long GetFactor(long n, int seed = 1)
        {
            if (n % 2 == 0)
                return 2;
            if (IsPrime(n))
                return n;
            long x = 2;
            long y = 2;
            long d = 1;
            long count = 0;
            while (d == 1)
            {
                count++;
                x = f(x, n, seed);
                y = f(f(y, n, seed), n, seed);
                d = Gcd(Math.Abs(x - y), n);
            }
            if (d == n)
                // 見つからなかった、乱数発生のシードを変えて再挑戦。
                return GetFactor(n, seed + 1);
            // 素数でない可能性もあるので、再度呼び出す
            return GetFactor(d);
        }

        private int[] seeds = new int[] { 3, 5, 7, 11, 13, 17 };
        private long f(long x, long n, int seed)
        {
            return (seeds[seed % 6] * x + seed) % n;
        }

        private static long Gcd(long a, long b)
        {
            if (a < b)
                return Gcd(b, a);  // 引数を入替えて自分を呼び出す
            if (b == 0)
                return a;
            long d = 0;
            do
            {
                d = a % b;
                a = b;
                b = d;
            } while (d != 0);
            return a;
        }

        // 効率は良くないが、これでも十分な速度がでたので、良しとする。
        private static bool IsPrime(long number)
        {
            long boundary = (long)Math.Floor(Math.Sqrt(number));

            if (number == 1)
                return false;
            if (number == 2)
                return true;

            for (long i = 2; i <= boundary; ++i)
            {
                if (number % i == 0)
                    return false;
            }

            return true;
        }
    }
}