#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  vector<int> arr(N);
  arr[1] = 1;
  for (int i = 2; i < N; i++) {
    if (arr[i] == 0) {
      for (int j = i; j < N; j += i) {
        arr[j] = i;
      }
    }
  }
  int n = 0;
  cin >> n;
  int ans = 1e9, tmp;
  int div = arr[n];
  for (int i = n - div + 1; i <= n; i++) {
    tmp = arr[i];
    if (tmp == i) tmp = 1;
    ans = min(ans, i - tmp + 1);
  }
  cout << ans;
}
