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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<vector>
#define inf 2147483647
#define mem0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=a;i<b;i++)
#define  rr read()
#define ll long long
#define pn putchar('\n')
#define ps putchar(' ')
#define repb(i,a,b) for(int i=a;i<=b;i++)
#define CloseIo   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
const int N=1e5+5;
bool is_prime(ll x){
    if(x==1)
        return false;
    if(x==2||x==3)
        return true;
    if(x%6!=1&&x%6!=5)
        return false;
    ll s=sqrt(x);
    for(int i=5;i<=s;i+=6)
        if(x%i==0||x%(i+2)==0)
            return false;
    return true;
}
void prime_diecom(ll n,vector<ll>&st){
    for(ll i=2;i<=n;i++){
        while(n!=i){
            if(n%i==0){
                st.push_back(i);
                n=n/i;
            }
            else
                break;
        }
        if(is_prime(n)){
           // st.insert(n);
            st.push_back(n);
            break;
        }
    }
}

int main()
{
vector<ll>v;
ll n;
scanf("%lld",&n);
prime_diecom(n,v);
if(v.size()==1)
{
	 printf("%lld\n",v[0]-1);
	 return 0;
}
multiset<ll>stt;
for(auto x:v)
stt.insert(x);

while(stt.size()>2)
{
	 auto tmp=stt.begin(),tmp2=stt.end();
	 tmp2--;
	 ll minx=(*tmp);
	 ll maxx=(*tmp2);
	 stt.erase(tmp);
	 stt.erase(tmp2);
	 stt.insert(minx*maxx); 
}
ll ans=0;
for(auto x:stt)
ans+=x;
printf("%lld\n",ans-2);
}