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 (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func main() {

	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	input, _ := strconv.Atoi(stdin.Text())
	if input >= 2 && input <= int(math.Pow(10, 12)) {
		// 素数判定
		if PrimeCheck(input) {
			input = input - 1
		} else {
			// 素因数分解
			factors := PrimeFactors(input)
			a := 1
			b := 1
			factorslen := len(factors)
			factorslenhalf := int(factorslen / 2)
			if factorslen%2 == 0 {
				for i := 0; i < factorslenhalf; i++ {
					if i == factorslenhalf-1 {
						a = a * factors[i]
						b = b * factors[factorslen-1-i]
					} else if i%2 == 0 {
						a = a * factors[i] * factors[factorslen-1-i]
					} else if i%2 == 1 {
						b = b * factors[i] * factors[factorslen-1-i]
					}
				}
			} else {
				for i := 0; i < factorslenhalf; i++ {
					if i == factorslenhalf-1 {
						a = a * factors[i]
						b = b * factors[factorslen-1-i]
						if a <= b {
							a = a * factors[i+1]
						} else {
							b = b * factors[i+1]
						}
					} else if i%2 == 0 {
						a = a * factors[i] * factors[factorslen-1-i]
					} else if i%2 == 1 {
						b = b * factors[i] * factors[factorslen-1-i]
					}
				}
			}
			input = (a - 1) + (b - 1)
		}
		fmt.Println(input)
	}
}

// 素数判定
func PrimeCheck(input int) bool {
	if input == 1 {
		return false
	} else if input == 2 {
		return true
	} else if input%2 == 0 {
		return false
	}

	primecheck := true
	rootx := int(math.Sqrt(float64(input)))
	i := 3
	for i <= rootx {
		if input%i == 0 {
			primecheck = false
			break
		}
		i += 2
	}
	return primecheck
	/*
		switch {
		case input < 2:
			return false
		case input == 2 || input == 3 || input == 5:
			return true
		case input & 1 == 0:
			return false
		case input % 2 == 0 || input % 3 == 0 || input % 5 == 0:
			return false
		}

		primecheck := true
		v := 7
		m := int(math.Pow(float64(input), 0.5)) + 1
		for v < m {
			if input % v == 0 {
				return false
			}
			if primecheck {
				v += 4
			} else {
				v += 2
			}
			primecheck = !primecheck
		}
		return true
	*/
}

// 素因数分解
func PrimeFactors(input int) []int {
	primefactors := make([]int, 0)
	i := 2
	for i*i <= input {
		r := input % i
		if r != 0 {
			i += 1
		} else if r == 0 {
			input /= i
			primefactors = append(primefactors, i)
		}
	}
	if input > 1 {
		primefactors = append(primefactors, input)
	}
	return primefactors
}