#include <bits/stdc++.h>
using namespace std;
const int MX = 1e6 + 9;
int x, p[MX], f[MX];
int main() {
  scanf("%d", &x);
  for (int i = 2; i < MX; i++) {
    if (p[i]) continue;
    for (int j = i + i; j < MX; j += i) {
      p[j] = 1;
      f[j] = i;
    }
  }
  int ans = x;
  for (int i = x - f[x] + 1; i <= x; i++) {
    ans = min(ans, i - f[i] + 1);
  }
  cout << ans << endl;
}
