#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int main() {
  int n, ans, d[N];
  bitset<N> b;
  vector<int> prime;
  scanf("%d", &n);
  b.reset();
  prime.push_back(2);
  for (int i = 2; i < N; i += 2) {
    d[i] = i - 1;
    d[i + 1] = i + 1;
  }
  for (int i = 3; i < N; i += 2)
    if (!b[i]) {
      prime.push_back(i);
      for (int j = i << 1; j < N; j += i) {
        b[j] = true;
        d[j] = j - i + 1;
      }
    }
  b.reset();
  ans = d[n];
  for (int i = d[n]; i < n; i++) ans = min(ans, d[i]);
  printf("%d\n", ans);
}
