#include <bits/stdc++.h>
using namespace std;
char a[1000][1000];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int n, m;
int prim[2][10000];
int k1[10000];
int main() {
  std::ios::sync_with_stdio(false);
  int i, j, k, t1, t2, t3;
  cin >> n;
  k = n;
  t1 = t2 = 0;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prim[0][t1++] = i;
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1) prim[0][t1++] = n;
  int s = k - prim[0][t1 - 1], t = k;
  t3 = 0x3f3f3f3f;
  for (i = s + 1; i <= t; i++) {
    int t4 = i;
    t2 = -1;
    for (j = 2; j * j <= t4; j++)
      if (t4 % j == 0) {
        t2 = max(t2, j);
        while (t4 % j == 0) t4 /= j;
      }
    if (t4 == i) continue;
    if (t4 != 1) t2 = max(t2, t4);
    t3 = min(t3, i + 1 - t2);
  }
  cout << t3;
}
