#include <bits/stdc++.h>
using namespace std;
bool si[1000007];
int x0 = 1000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x2;
  cin >> x2;
  si[0] = si[1] = false;
  for (int i = 2; i < 1000007; i++) {
    si[i] = true;
  }
  for (int i = 2; i < 1000007; i++) {
    if (si[i]) {
      for (int j = i * 2; j < 1000007; j += i) {
        si[j] = false;
      }
    }
  }
  int lp = 0;
  for (int i = 2; i <= sqrt(x2); i++) {
    if (x2 % i == 0) {
      if (si[i]) lp = max(lp, i);
      if (si[x2 / i]) lp = max(lp, x2 / i);
    }
  }
  for (int x1 = (x2 / lp - 1) * lp + 1; x1 <= x2; x1++) {
    for (int i = 2; i <= sqrt(x1); i++) {
      if (x1 % i == 0) {
        if (si[i]) {
          if (((x1 / i - 1) * i + 1) >= 3) x0 = min(x0, (x1 / i - 1) * i + 1);
        }
        if (si[x1 / i]) {
          if (((i - 1) * (x1 / i) + 1) >= 3)
            x0 = min(x0, (i - 1) * (x1 / i) + 1);
        }
      }
    }
  }
  cout << x0;
}
