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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)

	var evenCount = 0
	var nTemp = n
	for nTemp%2 == 0 {
		nTemp /= 2
		evenCount++
	}
	fmt.Fprintf(os.Stderr, "Error: evens %d\n", evenCount)
	var odds = make([]int, 0)
	var ii = 1
	for nTemp > 1 {
		ii += 2
		for nTemp%ii == 0 {
			nTemp /= ii
			odds = append(odds, ii)
		}
	}
	fmt.Fprintf(os.Stderr, "Error: odds %d\n", len(odds))

	var min = 0
	var evenNum1 int
	for evenNum1 = 0; evenNum1 <= evenCount; evenNum1++ {
		var evenNum2 = evenCount - evenNum1
		var pos int
		for pos = 0; pos < len(odds); pos++ {
			var num1 = 1
			if evenNum1 != 0 {
				for ii := 0; ii < evenNum1; ii++ {
					num1 *= 2
				}
			}
			for jj := 0; jj < pos; jj++ {
				num1 *= odds[jj]
			}

			var num2 = 1
			if evenNum2 != 0 {
				for ii := 0; ii < evenNum2; ii++ {
					num2 *= 2
				}
			}
			for jj := pos; jj < len(odds); jj++ {
				num2 *= odds[jj]
			}
			if min == 0 || min > num1+num2 {
				fmt.Fprintf(os.Stderr, "Error: nums %d, %d\n", num1, num2)
				min = num1 + num2
			}
		}

	}

	fmt.Printf("%d\n", min-2)
}