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

public class Main {


    public static int[] pref_f(String s) {
        int n = s.length();
        int[] pf = new int[n];
        for(int i = 1; i < n; i++){
            int j = pf[i-1];
            while (j > 0 && s.charAt(i) != s.charAt(j)){
                j = pf[j-1];
            }
            if (s.charAt(i) == s.charAt(j)) j++;
            pf[i] = j;
        }
        return pf;
    }

    public static void shortestPaths(List<Edge>[] edges, int s, int[] d, int[] pred) {
        Arrays.fill(pred, -1);
        Arrays.fill(d, Integer.MAX_VALUE);
        d[s] = 0;
        PriorityQueue<Long> q = new PriorityQueue<>();
        q.add((long) s);
        while (!q.isEmpty()) {
            long cur = q.remove();
            int curu = (int) cur;
            if (cur >>> 32 != d[curu])
                continue;
            for (Edge e : edges[curu]) {
                int v = e.t;
                int nprio = d[curu] + e.cost;
                if (d[v] > nprio) {
                    d[v] = nprio;
                    pred[v] = curu;
                    q.add(((long) nprio << 32) + v);
                }
            }
        }
    }

    public static class Edge {
        int t;
        int cost;

        public Edge(int t, int cost) {
            this.t = t;
            this.cost = cost;
        }
    }


    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        FastPrinter out = new FastPrinter(System.out);
        //FastScanner in = new FastScanner(new InputStreamReader(new FileInputStream(new File("strings.in"))));
        //FastPrinter out = new FastPrinter(new OutputStreamWriter(new FileOutputStream(new File("strings.out"))));
        ArrayList<Integer> list = new ArrayList<>();
        int n = in.nextInt();
        int[] mas = new int[1000000];
        for(int i = 2; i < 1000000; i++){
            if(mas[i] == 0){
                list.add(i);
                long k = (long)i * (long)i;
                while (k < 1000000){
                    mas[(int)k] = 1;
                    k += i;
                }
            }
        }
        int r = n-1;
        int max = 0;
        for(int i = 0; i < list.size(); i++){
            if(list.get(i) > n) break;
            if(n % list.get(i) == 0){
                max = list.get(i);
            }
        }
        int l = (n-max) + 1;

        max = r;
        for(int i = l; i <= r; i++){
            if(mas[i] == 0) continue;
            int k1 = 0;
            int k2 = i;
            for(int j = 0; j < list.size(); j++){
                int p = list.get(j);

                if((long)p * p > k2) break;

                if(k2 % p == 0){
                    k1 = p;
                    while (k2 % p == 0) k2 /= p;
                }

                if(k2 == p) {k1 = 1; break;}
            }
            if(k2 > k1) k1 = k2;
            if((i - k1 + 1) < max) max = (i - k1 + 1);
        }
        System.out.println(max);
        out.close();
    }
}

class FastPrinter {
    private final PrintWriter writer;

    public FastPrinter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public FastPrinter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void close() {
        writer.close();
    }

    public void println(long i) {
        writer.println(i);
    }

    public void println(double d) {
        writer.println(d);
    }

    public void println(String s) {
        writer.println(s);
    }

    public void println(String format, Object... args) {
        println(String.format(format, args));
    }

    public void println() {
        writer.println();
    }

    public void print(long i) {
        writer.print(i);
    }

    public void print(char c) {
        writer.print(c);
    }

    public void print(double d) {
        writer.print(d);
    }

    public void print(String s) {
        writer.print(s);
    }

    public void print(String format, Object... args) {
        print(String.format(format, args));
    }

    public void flush() {
        writer.flush();
    }
}

class FastScanner {

    private BufferedReader reader;
    private StringTokenizer st;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public FastScanner(InputStream stream) {
        this.reader = new BufferedReader(new InputStreamReader(stream));
        this.st = new StringTokenizer("");
    }

    public FastScanner(InputStreamReader stream) {
        this.reader = new BufferedReader(stream);
        this.st = new StringTokenizer("");
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public String next() {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(readLine());
        }
        return st.nextToken();
    }

    public String nextLine() {
        st = new StringTokenizer("");
        return readLine();
    }

    public String tryNextLine() {
        st = new StringTokenizer("");
        return tryReadLine();
    }

    private String readLine() {
        String line = tryReadLine();
        if (line == null) throw new InputMismatchException();
        return line;
    }

    private String tryReadLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public int[] nextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++)
            array[i] = nextInt();
        return array;
    }

    public int[][] nextIntTable(int rowCount, int columnCount) {
        int[][] table = new int[rowCount][];
        for (int i = 0; i < rowCount; i++)
            table[i] = nextIntArray(columnCount);
        return table;
    }

    public long[] nextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++)
            array[i] = nextLong();
        return array;
    }

    public long[][] nextLongTable(int rowCount, int columnCount) {
        long[][] table = new long[rowCount][];
        for (int i = 0; i < rowCount; i++)
            table[i] = nextLongArray(columnCount);
        return table;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\t' || isEOL(c);
    }

    public static boolean isEOL(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}
