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.*;
import java.util.*;


public class Main {

    void solve() throws IOException {
        int n = readInt();
        boolean simple[] = new boolean[(int) 1e6 + 1];
        int[] arr = new int[1000 * 1000 + 1];
        for (int i = 2; i < n; i++)
            if (!simple[i])
                for (int j = 2; j * i <= 1e6; j++) {
                    simple[j * i] = true;
                    arr[j * i] = maxInt(arr[j * i], i);
                }
        int size = 0;
        for (int i = 2; i <= 1e6; i++) if (!simple[i]) size++;
        simp = new int[size];
        int flag = 0;
        for (int i = 2; i <= 1e6; i++)
            if (!simple[i]) {
                simp[flag] = i;
                flag++;
            }
        TreeSet<Integer> del_n = getDel(n);
        HashSet<Integer> s = new HashSet<>();
        for (int i : simp) s.add(i);
        int[] dp = new int[1000 * 1000 + 1];
        Arrays.fill(dp, 1000 * 1000 * 1000);
        for (int i = n - del_n.pollLast() + 1; i <= n; i++) {
            if (i < 3) continue;
            int last = 0;
            if (s.contains(i)) {
                dp[i] = i;
                continue;
            }
            last = arr[i];
            if (i == last) dp[i] = i;
            else dp[i] = i - last + 1;
        }
        out.println(minInt(dp));
    }


    int[] simp;

    TreeSet<Integer> getDel(int a) {
        int f = 0;
        TreeSet<Integer> answer = new TreeSet<>();
        while (a > 1) {
            while (a % simp[f] == 0) {
                answer.add(simp[f]);
                a /= simp[f];
            }
            f++;
        }
        return answer;
    }

    boolean check(String a) {
        for (int i = 0; i < a.length() / 2; i++) {
            if (a.charAt(i) != a.charAt(a.length() - 1 - i)) {
                return false;
            }
        }
        return true;
    }


    long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    long binPow(long val, long pow) {
        if (pow == 0) return 1;
        if (pow % 2 == 1) {
            long k = binPow(val, pow / 2);
            return (k * k) * val;
        } else {
            long k = binPow(val, pow / 2);
            return (k * k);
        }
    }


    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    Main() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
        tok = new StringTokenizer("");
    }

    void run() throws IOException {
        solve();
        out.close();
    }


    class Fenwik {
        private long[] t;
        private int length;

        Fenwik(int[] a) {
            this.length = a.length + 100;
            this.t = new long[length];
            for (int i = 0; i < a.length; ++i) this.inc(i, a[i]);
        }

        void inc(int i, int delta) {
            for (; i < this.length; i = i | (i + 1)) t[i] += delta;
        }

        long getSum(int r) {
            long sum = 0;
            for (; r >= 0; r = (r & (r + 1)) - 1) sum += this.t[r];
            return sum;
        }

    }

    String delimiter = " ";

    String readLine() throws IOException {
        return in.readLine();
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            String nextLine = readLine();
            if (null == nextLine) return null;
            tok = new StringTokenizer(nextLine);
        }
        return tok.nextToken(delimiter);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    int[] readIntArray(int b) {
        int a[] = new int[b];
        for (int i = 0; i < b; i++)
            try {
                a[i] = readInt();
            } catch (IOException e) {
                e.printStackTrace();
            }
        return a;
    }

    int[] readSortedIntArray(int size) throws IOException {
        Integer[] array = new Integer[size];
        for (int index = 0; index < size; ++index) array[index] = readInt();
        Arrays.sort(array);
        int[] sortedArray = new int[size];
        for (int index = 0; index < size; ++index) sortedArray[index] = array[index];
        return sortedArray;
    }

    int[] sortedIntArray(int size, int[] array) throws IOException {
        for (int index = 0; index < size; ++index) array[index] = readInt();
        Arrays.sort(array);
        int[] sortedArray = new int[size];
        for (int index = 0; index < size; ++index) sortedArray[index] = array[index];
        return sortedArray;
    }

    int minInt(int... values) {
        int min = Integer.MAX_VALUE;
        for (int value : values) min = Math.min(min, value);
        return min;
    }

    int maxInt(int... values) {
        int max = Integer.MIN_VALUE;
        for (int value : values) max = Math.max(max, value);
        return max;
    }

    long minLong(long... values) {
        long min = Long.MAX_VALUE;
        for (long value : values) min = Math.min(min, value);
        return min;
    }

    long maxLong(long... values) {
        long max = Long.MIN_VALUE;
        for (long value : values) max = Math.max(max, value);
        return max;
    }

    boolean checkIndex(int index, int size) {
        return (0 <= index && index < size);
    }

    int abs(int a) {
        if (a < 0) return -a;
        return a;
    }
}
