#include <bits/stdc++.h>
using namespace std;
const int maxn = (1e6) + 15;
const int mod = (1e9) + 7;
const int inf = (1e9) + 3;
int n, ans = inf;
string s;
long long int a[maxn];
bool mark[maxn], isprime[maxn];
vector<int> v[maxn];
void solve2(int x) {
  for (int u : v[x]) {
    int y = x / u;
    y--;
    ans = min(ans, (u * y) + 1);
  }
}
void solve1(int x) {
  int y = n / x;
  y--;
  for (int i = (x * y) + 1; i < n; i++)
    if (!isprime[i]) solve2(i);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  for (int i = 2; i < maxn; i++)
    if (!mark[i])
      for (int j = 1; j * i < maxn; j++)
        mark[i * j] = 1, isprime[i] = 1, v[i * j].push_back(i);
  cin >> n;
  for (int u : v[n]) solve1(u);
  if (ans == inf)
    for (int i = n; i >= 0; i--)
      if (isprime[i]) cout << i << endl, exit(0);
  cout << ans << endl;
  return 0;
}
