#include <bits/stdc++.h>
using namespace std;
int spf[1000002];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < 1000002; i++) spf[i] = i;
  for (int i = 4; i < 1000002; i += 2) spf[i] = 2;
  for (int i = 3; i * i < 1000002; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < 1000002; j += i) {
        if (spf[j] == j) spf[j] = i;
      }
    }
  }
}
vector<int> getFactors(int x) {
  vector<int> v;
  while (x != 1) {
    if (spf[x] == 1) break;
    v.push_back(spf[x]);
    int last = spf[x];
    while (spf[x] == last) x /= spf[x];
  }
  return v;
}
int main() {
  sieve();
  int sm = INT_MAX;
  int x;
  cin >> x;
  vector<int> v = getFactors(x);
  for (int i = 0; i < v.size(); i++) {
    int p = v[i];
    int d = x / p;
    for (int j = p * (d - 1) + 1; j <= x; j++) {
      vector<int> v1 = getFactors(j);
      for (int k = 0; k < v1.size(); k++) {
        {
          int x0 = v1[k] * (j / v1[k] - 1) + 1;
          if (x0 == 1) continue;
          if (x0 < sm) sm = x0;
        }
      }
    }
  }
  cout << sm << endl;
}
