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.awt.image.AreaAveragingScaleFilter;
import java.io.*;
import java.util.*;

import static java.lang.Math.*;


public class Main {
    BufferedReader br;
    StringTokenizer in;
    PrintWriter pw;
    Random r;
    int INF = (int) 1e9;
    long LNF = (long) 1e18;
    long mod = (long) (1e9 + 7);
    int pp = 27;
    // you shall not hack!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //        /*\
    //       ///          /^\
    //      ///    ~~~~  (___)
    //     ///   ~~~~~~~~  \\__
    //   _/_/_  ~/ .& . \~  \\ \
    //  [____] ~|  (__)  |~/ \\/
    //  /_/\  \~|~~~~~~~~~~  /\\
    //      \   ~~~~~~~~~~ _/ \\
    //       \ _    ~~   _/\   \\
    //      /  |        /   \   \\
    //     |  /        |     \   \\
    //    /  /   ___   |      \
    //   /___|  | __|  |_______\
    //      _|  |   |  |_
    //     |____|   |____|
    //       P.S this is omnipotent Gandalf

    ArrayList<Integer> getd(int x) {
        int xx = x;
        ArrayList<Integer> ans = new ArrayList<>();
        for (int i = 2; i * i <= x; i++) {
            while (xx % i == 0) {
                ans.add(i);
                xx /= i;
            }
        }
        if (xx != 1) ans.add(xx);
        return ans;
    }

    int getmaxd(int x) {
        return prime[x];
    }

    int prime[] = new int[(int) (1e6 + 1)];

    void solve() throws IOException {
        int x2 = nextInt();
        ArrayList<Integer> div2 = getd(x2);
        int ans = (int) 1e9;
        Arrays.fill(prime, 1);
        prime[0] = 0;
        prime[1] = 0;
        for (int i = 2; i < 1e6 + 1; i++) {
            if (prime[i] == 1) {
                for (int j = i + i; j <= 1e6; j += i) {
                    prime[j] = i;
                }
            }
        }

        for (int i = 0; i < div2.size(); i++) {
            int p = div2.get(i);
            int y = x2 / p;
            int minx1 = p * (y - 1) + 1;
            int maxx1 = x2;
            for (int j = minx1; j <= maxx1; j++) {
                int x1 = j;
                int d = getmaxd(x1);
                if (d != x1) {
                    int yy = x1 / d;
                    int x0 = d * (yy - 1) + 1;
                    ans = min(ans, x0);
                }
            }
        }
        pw.print(ans);

    }

    class event implements Comparable<event> {
        int time;
        int t;
        int a;

        public event(int ttime, int tt, int aa) {
            time = ttime;
            t = tt;
            a = aa;
        }

        @Override
        public int compareTo(event o) {
            if (time != o.time) return time - o.time;
            return o.t - t;
        }
    }

    //***************************************
    class road {
        int to;

        int w;

        public road(int tto, int ww) {
            to = tto;
            w = ww;
        }
    }

    class pair implements Comparable<pair> {
        int a;

        long b;

        public pair(int aa, long bb) {
            a = aa;
            b = bb;
        }


        @Override
        public int compareTo(pair o) {
            if (b != o.b) return Long.compare(b, o.b);
            return a - o.a;
        }
    }


    Comparator<road> byw = new Comparator<road>() {
        @Override
        public int compare(road o1, road o2) {
            if (o1.w != o2.w) return Integer.compare(o1.w, o2.w);
            return Integer.compare(o1.to, o2.to);
        }
    };
    Comparator<road> byto = new Comparator<road>() {
        @Override
        public int compare(road o1, road o2) {
            return Integer.compare(o1.to, o2.to);
        }
    };

    class node {
        int sz;
        int val;
        //        long sum;
        node L;
        node R;
        int prior;

        public node(int val) {
            this.val = val;
            prior = r.nextInt();
            L = null;
            R = null;
            sz = 1;
//            sum = (long)val * val;
        }

        void add(int z) {
            val = val + z;
//            sum = (long)val * val;
        }
    }

    void relax(node c) {
        if (c == null) return;
        c.sz = sfsz(c.L) + sfsz(c.R) + 1;
//        c.sum = sfsum(c.L) + sfsum(c.R) + c.val * c.val;
    }

    int sfsz(node c) {
        if (c == null) return 0;
        return c.sz;
    }

    node merge(node a, node b) {
        if (a == null) return b;
        if (b == null) return a;
        if (a.prior > b.prior) {
            a.R = merge(a.R, b);
            relax(a);
            return a;
        } else {
            b.L = merge(a, b.L);
            relax(b);
            return b;
        }
    }

    pairn split(node c, int s) {
        if (c == null) return new pairn(null, null);
        if (sfsz(c.L) >= s) {
            pairn p = split(c.L, s);
            c.L = p.b;
            relax(c);
//            relax(p.a);
            return new pairn(p.a, c);
        } else {
            pairn p = split(c.R, s - sfsz(c.L) - 1);
            c.R = p.a;
            relax(c);
//            relax(p.b);
            return new pairn(c, p.b);
        }
    }

    class pairn {
        node a;
        node b;

        public pairn(node a, node b) {
            this.a = a;
            this.b = b;
        }

    }

    String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    void gener() throws IOException {
        pw = new PrintWriter(new FileWriter("input.txt"));

        pw.close();
    }

    public void run() throws IOException {
        r = new Random(5);
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
//        br = new BufferedReader(new FileReader("input.txt"));
//        pw = new PrintWriter(new FileWriter("output.txt"));
//        br = new BufferedReader(new FileReader("holidays.in"));
//        pw = new PrintWriter(new FileWriter("holidays.out"));
        solve();
        pw.close();
    }

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