Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try to reach one million by the process described below. 

Alice goes first and then they take alternating turns. In the i-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.

Formally, he or she selects a prime p < Xi - 1 and then finds the minimum Xi ≥ Xi - 1 such that p divides Xi. Note that if the selected prime p already divides Xi - 1, then the number does not change.

Eve has witnessed the state of the game after two turns. Given X2, help her determine what is the smallest possible starting number X0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions.

Input

The input contains a single integer X2 (4 ≤ X2 ≤ 106). It is guaranteed that the integer X2 is composite, that is, is not prime.

Output

Output a single integer — the minimum possible X0.

Examples

Input

14


Output

6


Input

20


Output

15


Input

8192


Output

8191

Note

In the first test, the smallest possible starting number is X0 = 6. One possible course of the game is as follows: 

  * Alice picks prime 5 and announces X1 = 10
  * Bob picks prime 7 and announces X2 = 14. 



In the second case, let X0 = 15. 

  * Alice picks prime 2 and announces X1 = 16
  * Bob picks prime 5 and announces X2 = 20. 
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author AEoui
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        ArrayList<Integer> primes;
        ArrayList<Integer> facors;
        int[] pf;

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            init();
            int X2 = in.nextInt();

            facors(X2);

            int low = 3;
            int hi = X2 - 1;
            int mid = 0;
            int ans = -1;
            while (low <= hi) {
                mid = (low + hi) / 2;
                if (check(mid, X2)) {
                    ans = mid;
                    hi = mid - 1;
                } else {
                    low = mid + 1;
                }
            }

            out.println(ans);
        }

        private void facors(int x2) {
            facors = new ArrayList<>();
            int cur = pf[x2];
            while (cur > 1) {
                facors.add(pf[cur]);
                int ppf = pf[cur];
                while (cur % ppf == 0)
                    cur /= ppf;
            }

            if (cur > 1)
                facors.add(cur);

            Collections.sort(facors);
        }

        private boolean check(int x0, int x2) {
            for (Integer p : primes) {
                if (p >= x0) break;

                int x1 = (x0 / p) * p;
                x1 += x1 < x0 ? p : 0;

                for (int p1 : facors) {
                    if (p1 >= x1) break;
                    if (x2 % p1 != 0) continue;
                    int xx2 = (x1 / p1) * p1;
                    xx2 += xx2 < x1 ? p1 : 0;

                    if (xx2 == x2)
                        return true;
                }
            }

            return false;
        }

        private void init() {
            pf = new int[(int) 1e6 + 1];
            primes = new ArrayList<>();

            for (int i = 2; i < pf.length; ++i) {
                if (pf[i] != 0) continue;
                primes.add(i);
                pf[i] = i;
                for (int j = i + i; j < pf.length; j += i) {
                    pf[j] = i;
                }
            }
        }

    }
}

