#include <bits/stdc++.h>
static int s[1000002], p[1000002], np, min;
static void sieve(void) {
  int i, j;
  for (i = 2; i < 1000002; i++) {
    if (s[i] == 0) {
      p[np++] = i;
      for (j = 2; i * j < 1000002; j++) {
        s[i * j] = 1;
      }
    }
  }
}
static void solve(int x, int l) {
  int i, d, lf = -1;
  if (x < min) {
    min = x;
  }
  if (l == 0) {
    return;
  }
  for (i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      if (s[x / i] == 0) {
        if (x / i > lf) {
          lf = x / i;
        }
      } else if (s[i] == 0) {
        if (i > lf) {
          lf = i;
        }
      }
    }
  }
  if (lf == -1) {
    return;
  }
  if (l == 1) {
    if (lf * (x / lf - 1) + 1 < min) {
      min = lf * (x / lf - 1) + 1;
    }
  } else {
    for (d = lf * (x / lf - 1) + 1; d < x; d++) {
      solve(d, l - 1);
    }
  }
}
int main(void) {
  int x2;
  sieve();
  min = INT_MAX;
  scanf("%d", &x2);
  solve(x2, 2);
  printf("%d", min);
  return 0;
}
