#include <bits/stdc++.h>
using namespace std;
int prime[1000100];
void get_prime() {
  for (int i = 2; i < 1000100; i++) {
    if (!prime[i]) {
      for (int j = i * 2; j < 1000100; j += i) {
        prime[j] = i;
      }
    }
  }
  for (int i = 2; i < 1000100; i++) {
    if (!prime[i]) {
      prime[i] = 1;
    }
  }
}
int main() {
  get_prime();
  int x2;
  cin >> x2;
  int ans = 0x3f3f3f3f3f;
  for (int i = x2 - prime[x2] + 1; i <= x2; i++) {
    ans = min(ans, i - prime[i] + 1);
  }
  cout << ans;
}
