#include <bits/stdc++.h>
using namespace std;
const int RNG = 1000003;
const int RNGT = RNG * 4;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 87;
const double pi = 2 * acos(0.0);
inline int _Int() {
  int x;
  scanf("%d", &x);
  return x;
}
inline long long _LLi() {
  long long x;
  scanf("%lld", &x);
  return x;
}
void pruts() {
  puts("-1");
  exit(EXIT_SUCCESS);
}
int dirX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dirY[] = {0, 1, 0, -1, 1, -1, -1, 1};
int rX[] = {1, 1, 2, 2, -1, -1, -2, -2};
int rY[] = {2, -2, 1, -1, 2, -2, 1, -1};
template <class T>
T tri_Area(T x1, T y1, T x2, T y2, T x3, T y3) {
  return abs(x1 * (y2 - y3) - y1 * (x2 - x3) + (x2 * y3 - x3 * y2));
};
template <class T>
T Distance(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
};
template <class T>
T bigMod(T n, T p, T m) {
  if (p == 0) return 1;
  if (p & 1) return (n * bigMod(n, p - 1, m)) % m;
  T x = bigMod(n, p / 2, m);
  return (x * x) % m;
};
int Case;
int sset(int N, int pos) { return N = N | (1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
vector<int> pd, nn;
int flag[RNG];
int prime[RNG];
void prim() {
  int sq = sqrt(RNG);
  for (int i = 2; i <= sq; i++) {
    if (!prime[i]) {
      for (int j = i + i; j < RNG; j += i) prime[j] = 1;
    }
  }
}
void pr_dv(int x) {
  while (!(x % 2)) {
    if (!flag[2]) {
      pd.push_back(2);
      flag[2] = 1;
    }
    x /= 2;
  }
  for (int i = 3; i * i <= x; i += 2) {
    while (!(x % i)) {
      if (!flag[i]) {
        flag[i] = 1;
        pd.push_back(i);
      }
      x /= i;
    }
  }
  if (x > 1) pd.push_back(x);
}
int pd2(int x) {
  int mx = 0;
  while (!(x % 2)) {
    mx = max(mx, 2);
    x /= 2;
  }
  for (int i = 3; i * i <= x; i += 2) {
    while (!(x % i)) {
      mx = max(mx, i);
      x /= i;
    }
  }
  if (x > 1) mx = max(mx, x);
  return mx;
}
void Hunger() {
  int n = _Int();
  pr_dv(n);
  int sz = pd.size();
  int ans = INT_MAX;
  for (int i = 0; i < sz; i++) {
    int x = n / pd[i];
    int y = x - 1;
    int m = y * pd[i];
    for (int j = m + 1; j < n; j++) {
      if (!prime[j] && j > 2)
        ans = min(ans, j);
      else
        nn.push_back(j);
    }
  }
  sz = nn.size();
  for (int i = 0; i < sz; i++) {
    int mx = pd2(nn[i]);
    int mxx = nn[i] / mx;
    int mxy = mxx - 1;
    int pp = mxy * mx;
    if (pp + 1 > 2) ans = min(ans, pp + 1);
  }
  cout << ans << endl;
}
int main() {
  prim();
  Hunger();
  return 0;
}
