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.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author chaku
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
                int n=in.nextInt();
                int[] prime=new int[n+1];
                for(int i=2;i*i<=n;i++)
                {
                    if(prime[i]==0)
                    {
                        for(int j=2*i;j<=n;j+=i)
                        {
                            prime[j]=1;
                        }
                    }
                }
                int x1=n;
                for(int i=n-1;i>1;i--)
                {
                    if(prime[i]==0 && n%i==0)
                    {
                        x1=n-i+1;break;
                    }
                }
                int ans=n;
                for(int i=2;i<n;i++)
                {
                    if(prime[i]==0)
                    {
                        int x=i*((x1+(i-1))/i);
                        if(x<=n && i<x)
                        {
                          ans=Math.min(ans,x-i+1);  
                        }
                    }
                }
                out.println(ans);
        }
        
        static int maxFac(int n)
        {
            int max=2;
                for(int i=2;i*i<=n;i++)
                {
                    if(n%i==0)
                    {
                        if(checkPrime(i))
                        {
                            max=Math.max(i,max);
                        }
                        if(checkPrime(n/i))
                        {
                           max=Math.max(n/i,max);
                        }
                    }
                }
            return max;
        }
        
        static boolean checkPrime(int n)
        {
            for(int i=2;i*i<=n;i++)
            {
                if(n%i==0)return false;
            }
            return true;
        }
        
        public static int cmp(Pair p1,Pair p2)
        {
                long l=(long)p1.a*p2.b-(long)p1.b*p2.a;
                if(l>0)return 1;
                else if(l<0)return -1;
                return 0;
        }
        static int bit[];
        
        static void add(int x,int d,int n)
        {
            for(int i=x;i<=n;i+=i&-i)bit[i]+=d;
        }

        static int query(int x)
        {
            int ret=0;
            for(int i=x;i>0;i-=i&-i)ret+=bit[i];
            return ret;
        }
        
        static class Pair
        {
                int a;int b;
                public Pair(int a,int b)
                {
                        this.a=Math.abs(a);this.b=Math.abs(b);
                }
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

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

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

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

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

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

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {

            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

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

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

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

        }

    }
}