#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int>::iterator it, it2;
int mi = -1;
int main() {
  bool e = true;
  int i, k;
  int sum;
  for (i = 2; i < 500000; i++) {
    e = true;
    for (it = v.begin(); it != v.end() && *it <= i / (*it); it++) {
      if (i % *it == 0) {
        e = false;
      }
    }
    if (e == true) v.push_back(i);
  }
  int a, a2;
  scanf("%d", &a);
  for (it = v.begin(); it != v.end() && *it <= a / 2; it++) {
    if (a % *it == 0) {
      a2 = a - *it;
      it2 = it;
    }
  }
  for (it = v.begin(); it != v.end() && *it < a2; it++) {
    sum = ((a2 / (*it)) + 1) * (*it);
    if (sum <= a) {
      if (mi == -1 || mi > a2 / (*it) * (*it)) {
        mi = a2 / (*it) * (*it) + 1;
      }
    }
  }
  printf("%d", mi == -1 ? a2 + 1 : mi);
}
