#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int x = n;
  vector<int> dels2;
  vector<int> dels;
  map<int, int> arr;
  for (int i = 2; i <= sqrt((long double)n); ++i) {
    while (x % i == 0) {
      dels.push_back(i);
      x /= i;
    }
  }
  if (x != 1) {
    dels.push_back(x);
  }
  int minPos = 1e9;
  for (int i = (n / dels[dels.size() - 1] - 1) * dels[dels.size() - 1] + 1;
       i <= n; ++i) {
    int z = i;
    int count = 0;
    for (int k = 2; k <= sqrt((long double)z); ++k) {
      while (z % k == 0) {
        z /= k;
        count++;
        if (((i / k) - 1) * k < minPos) minPos = ((i / k) - 1) * k;
      }
    }
    if (z != 1 && count > 0) {
      if ((i / z - 1) * z < minPos) minPos = (i / z - 1) * z;
    }
  }
  cout << minPos + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(9);
  solve();
  return 0;
}
