#include <bits/stdc++.h>
using namespace std;
bool judge[1000010];
int num[1000010];
int main() {
  int x2;
  while (scanf("%d", &x2) != EOF) {
    memset(judge, true, sizeof(judge));
    judge[0] = judge[1] = false;
    for (int i = 2; i < x2; i++) {
      if (judge[i]) {
        for (int j = i + i; j <= x2; j += i) {
          judge[j] = false;
          num[j] = i;
        }
      }
    }
    int ans = 0x3f3f3f3f3f3f3f3f;
    for (int i = x2 - num[x2] + 1; i <= x2; i++) {
      ans = min(ans, i - num[i] + 1);
    }
    printf("%d\n", ans);
  }
  return 0;
}
