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 PHP7 (7.0.15) (judge verdict: Wrong Answer)

<?php
//error_reporting(0);
$N = trim(fgets(STDIN));
//list($A,$B) =  explode(" ",trim(fgets(STDIN)));
//$l = explode(" ",trim(fgets(STDIN)));


$pf = prime_factors($N);
$cnt = count($pf);
if (count($pf) == 1) {
  printf("%d\n",$N-1);
  exit;
}
//組み合わせのうち最も数値が小さいものを調べる
for($i=0;$i<$cnt-1;$i++) {
  $res[]=getkekka($pf,$i,true)+getkekka($pf,$i,false);
}
sort($res);
printf("%d\n",$res[0]-2);
function getkekka($pf,$pos,$start) {
  $res = 1;
  if ($start == true) {
    for($i=0;$i<=$pos;$i++) {
      $res *= $pf[$i];
    }
  }
  else {
    for($i=$pos+1;$i<count($pf);$i++) {
      $res *= $pf[$i];
    }
  }
 return $res;
}

function gcd($a,$b) {
  if ($b == 0) return $a;
  return gcd($b,$a%$b);
}
function lcm($a,$b) {
  $l = gcd($a,$b);
  return $a / $l * $b;
}

function prime_factors(int $n) {
    $i = 2;
    $factors = [];
    while ($i * $i <= $n) {
        if ($n % $i) $i += 1;
        else {
            $n /= $i;
            $factors[] = $i;
        }
    }
    if ($n > 1) $factors[] = $n;
    return $factors;
}
function is_prime(int $n) : bool {
    $r = true;
    if ($n < 2) $r = false;
    elseif ($n == 2 || $n == 3 || $n == 5) $r = true;
    elseif ($n & 1 == 0) $r = false;
    elseif ($n % 2 == 0 || $n % 3 == 0 || $n % 5 == 0) $r = false;
    else {
        $f = true;
        $v = 7;
        $m = intval($n ** 0.5) + 1;
        while ($v < $m) {
            if ($n % $v == 0) {
                $r = false;
                break;
            }
            $v += $f ? 4 : 2;
            $f = !$f;
        }
    }
    return $r;
}