#include <bits/stdc++.h>
using namespace std;
int pos[1000050];
bool vis[1000050];
int main() {
  for (int i = 0; i <= 1000050; i++) pos[i] = i;
  for (int i = 2; i <= 1000050; i++) {
    if (!vis[i]) {
      pos[i] = i;
      for (int j = i + i; j <= 1000050; j += i) {
        vis[j] = 1;
        pos[j] = min(pos[j], j - i + 1);
      }
    }
  }
  int x;
  cin >> x;
  int ans = x;
  for (int i = pos[x]; i <= x; i++) {
    ans = min(ans, pos[i]);
  }
  cout << max(ans, 3) << endl;
  return 0;
}
