#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int N = 1e6 + 5;
int Find[N];
int vis[N];
void init() {
  for (int i = 2; i < N; i++) {
    if (!vis[i]) {
      Find[i] = -1;
      for (int j = i * 2; j < N; j += i) vis[j] = 1, Find[j] = i;
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int ans = n;
  init();
  for (int i = n - Find[n] + 1; i <= n; i++) ans = min(ans, i - Find[i] + 1);
  printf("%d", ans);
  return 0;
}
