#include <bits/stdc++.h>
using namespace std;
int x;
int flg[1000006];
vector<int> prime;
int main() {
  for (int i = 2; i < 1000006; i++) {
    if (flg[i]) continue;
    for (int j = i; j < 1000006; j += i) flg[j] = max(flg[j], i);
  }
  for (int i = 2; i < 1000006; i++) {
    if (flg[i] == i) continue;
    flg[i] = i - flg[i] + 1;
  }
  cin >> x;
  int ans = 1e9;
  for (int i = flg[x]; i <= x; i++) {
    ans = min(ans, flg[i]);
  }
  cout << ans << endl;
  return 0;
}
