#include <bits/stdc++.h>
using namespace std;
bool p[1005];
int s[1005];
int cnt = 0;
int n;
void Get() {
  for (int i = 2; i <= 1000; i++) {
    if (p[i] == 0) {
      for (int j = i; j <= 1000; j += i) p[j] = 1;
      s[++cnt] = i;
    }
  }
  return;
}
int find(int x) {
  int u;
  for (int i = 1; i <= cnt; i++) {
    while (x % s[i] == 0) {
      x /= s[i];
      u = s[i];
    }
  }
  return x == 1 ? u : x;
}
int main() {
  scanf("%d", &n);
  Get();
  int w = find(n);
  int x = 1e9 + 7;
  for (int i = n; i > w * (n / w - 1); i--) {
    int t = find(i);
    if (t * (i / t - 1) + 1 <= x && t != i) x = t * (i / t - 1) + 1;
  }
  cout << x << endl;
  return 0;
}
