#include <bits/stdc++.h>
using namespace std;
int find_max(int r) {
  int max = 0, a = r;
  for (int i = 2; i * 1ll * i <= r; i++) {
    if (a % i == 0) max = i;
    while (a % i == 0 && a > 1) {
      a /= i;
    }
  }
  if (a > max) max = a;
  return max;
}
int main() {
  int max, a, l, r, current, min = 1000001;
  cin >> a;
  l = a - find_max(a) + 1;
  for (int i = l; i <= a; i++) {
    if (min < i / 2) break;
    max = find_max(i);
    if (max != i) {
      current = i - max + 1;
      if (current < min) min = current;
    }
  }
  cout << min;
  return 0;
}
