#include <bits/stdc++.h>
using namespace std;
int x2;
int find(int n) {
  int ans = 0;
  int temp = n;
  for (int i = 2; i <= n; i++) {
    if (i * i > n) {
      if (n != temp) ans = n;
      break;
    }
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      ans = max(ans, i);
    }
  }
  return ans;
}
int main() {
  cin >> x2;
  int first = find(x2);
  int ans = 0x3f3f3f3f;
  for (int i = x2; i > x2 - first; i--) {
    ans = min(ans, i - find(i) + 1);
  }
  cout << ans << endl;
}
