#include <bits/stdc++.h>
using namespace std;
bool check[1000005];
int prime[1000005];
int main() {
  memset(check, false, sizeof(check));
  int pos = 0;
  for (int i = 2; i <= 1000005; i++) {
    if (!check[i]) prime[++pos] = i;
    for (int j = 1; j <= pos && i * prime[j] < 1000005; j++) {
      check[i * prime[j]] = true;
      if (i % prime[j] == 0) break;
    }
  }
  int x2;
  scanf("%d", &x2);
  for (int i = pos; i >= 1; i--) {
    if (x2 % prime[i] == 0) {
      int minn = 1000005, tmp = x2 - prime[i] + 1;
      for (int j = 1; j <= pos; j++) {
        if (x2 / prime[j] != (tmp - 1) / prime[j] &&
            (tmp - 1) / prime[j] != 0) {
          minn = min(minn, (tmp - 1) / prime[j] * prime[j] + 1);
        }
      }
      printf("%d\n", minn);
      return 0;
    }
  }
  return 0;
}
