#include <bits/stdc++.h>
using namespace std;
const int N = 1e+6 + 5;
int lp[N], ans = 1e+9;
set<int> factors;
void factorise(int n, int isv) {
  int cnt = 0, ans = 0, n1 = n;
  while (n % 2 == 0) {
    cnt++;
    n = n / 2;
  }
  if (cnt) {
    ans = max(ans, 2);
    if (isv) factors.insert(2);
  }
  for (int i = 3; i * i <= n; i++) {
    cnt = 0;
    while (n % i == 0) {
      cnt++;
      n = n / i;
    }
    if (cnt) {
      ans = max(ans, i);
      if (isv) factors.insert(i);
    }
  }
  if (n > 1) {
    ans = max(ans, n);
    if (isv) factors.insert(n);
  }
  lp[n1] = ans;
}
int main() {
  int x;
  cin >> x;
  for (int i = 2; i <= x; i++) factorise(i, i == x);
  for (auto it : factors) {
    int i = x - it + 1;
    for (i; i <= x; i++) {
      if (lp[i] != i) ans = min(ans, i - lp[i] + 1);
    }
  }
  cout << ans;
}
