#include <bits/stdc++.h>
using namespace std;
vector<long long> prims;
void primes(long long n) {
  while (n % 2 == 0) {
    prims.push_back(2);
    n = n / 2;
  }
  for (long long i = 3; i <= sqrt(n); i += 2) {
    while (n % i == 0) {
      prims.push_back(i);
      n = n / i;
    }
  }
  if (n > 2) prims.push_back(n);
}
int main() {
  long long n;
  cin >> n;
  long long ans = 1e17;
  primes(n);
  for (long long i = 0; i < (long long)(prims.size()); i++) {
    ans = min(ans, abs(prims[i] + n / prims[i] - 2));
  }
  if (ans == 1e17)
    cout << 0 << '\n';
  else
    cout << ans << '\n';
  return 0;
}
