#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return false;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return true;
}
template <class T>
inline void output(T answer) {
  if (answer < 0) {
    putchar('-');
    answer = -answer;
  }
  if (answer >= 10) output(answer / 10);
  putchar(answer % 10 + '0');
  return;
}
inline long long addMod(long long x, long long y, long long mod) {
  x += y;
  if (x > mod) x -= mod;
  return x;
}
inline long long minusMod(long long x, long long y, long long mod) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline long long qmul(long long, long long, long long);
inline long long qpow_qmul(long long, long long, long long);
inline long long mul(long long, long long, long long);
inline long long qpow(long long, long long, long long);
inline long long gcd(long long, long long);
inline long long qpow_mul(long long, long long, long long);
const int N = 1e6 + 6;
const int M = 1e3 + 10;
const int INF = 1e9 + 7;
const long long mod = 998244353;
const double PI = acos(-1.0);
const double esp = 1e-7;
const int MAXN = 2e5 + 5;
vector<int> g[N];
bool vis[N];
void pre(int n) {
  for (int i = 2; i <= n; i++) {
    if (!vis[i]) {
      for (int j = i + i; j <= n; j += i) {
        vis[j] = true;
        g[j].push_back(i);
      }
    }
  }
}
int main() {
  int n, left, right, k;
  scanf("%d", &n);
  pre(n);
  k = n / g[n].back();
  left = max((k - 1) * g[n].back() + 1, 3);
  right = n;
  int ans = n;
  for (int i = left; i <= right; i++) {
    if (!vis[i]) continue;
    ans = min(ans, (i / g[i].back() - 1) * g[i].back() + 1);
  }
  printf("%d\n", ans);
  return 0;
}
inline long long mul(long long a, long long b, long long MOD) {
  long long L = a * (b >> 25ll) % MOD * (1ll << 25) % MOD;
  long long R = a * (b & ((1ll << 25) - 1)) % MOD;
  return (L + R) % MOD;
}
inline long long qpow_mul(long long x, long long n, long long MOD) {
  long long ans = 1, a = x % MOD;
  while (n) {
    if (n & 1) {
      ans = mul(ans, a, MOD);
    }
    a = mul(a, a, MOD);
    n >>= 1;
  }
  return ans;
}
inline long long qmul(long long a, long long b, long long MOD) {
  long long ans = 0, x = a;
  while (b) {
    if (b & 1) {
      ans = (ans + x) % MOD;
    }
    x = x * 2 % MOD;
    b >>= 1;
  }
  return ans % MOD;
}
inline long long qpow_qmul(long long x, long long n, long long MOD) {
  long long ans = 1, a = x % MOD;
  while (n) {
    if (n & 1) {
      ans = qmul(ans, a, MOD);
    }
    a = qmul(a, a, MOD);
    n >>= 1;
  }
  return ans;
}
inline long long qpow(long long x, long long n, long long MOD) {
  if (x == 1) return 1;
  long long ans = 1, a = x;
  while (n) {
    if (n & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    n >>= 1;
  }
  return ans;
}
inline long long gcd(long long m, long long n) {
  return n == 0 ? m : gcd(n, m % n);
}
