x = int(input())
lp = [0 for x in range(x+1)]
for i in range(2, x+1):
    if(lp[i]==0):
        for j in range(i*2, x+1, i):
            lp[j] = i
    lp[i] = i - lp[i] + 1
p = x
for i in range(lp[x], x+1):
    p = min(p, lp[i])
print(p)

a = []
for i in range(1000005):
    a.append(0)

n = int(input())
ans = n

for i in range(2,n+1):
    if not a[i]:
        j = 2*i
        while(j <= n):
            a[j] = i
            j+=i
    a[i] = i - a[i] +1

#print (a)

for i in range(a[n],n+1):
    ans = min(ans,a[i])
    #print (ans)

print(ans)

x2 = int(input())
num = []
for val in range(1000010):
    num.append(0)
for i in range(2,x2):
    if(num[i]==0):
        for j in range(i+i,x2+1,i):
            num[j]=i
prim = x2 - num[x2] + 1
minn = 10000000
for i in range(prim,x2+1):
    minn = min(minn,i - num[i] + 1)
print(minn)

n = int(input())
ans = n
f = [0]*(n+1)
for i in range(2, n+1):
    if f[i]==0:
        for j in range(i*2, n+1, i):
            f[j] = i
    f[i] = i-f[i]+1
for i in range(f[n], n+1):
    ans = min(ans, f[i])
print(ans)

def prime_factors(n):
    f = 2
    while f*f<=n:
        while n%f == 0:
            yield f
            n //= f
        f += 1
    if n>1:
        yield n
x2 = int(input())
ans = x2
# print([x for x in prime_factors(x2)])
for x1 in range(x2-max([x for x in prime_factors(x2)])+1, x2+1):
    # print(x1, [x for x in prime_factors(x1)])
    tmp = x1-max([x for x in prime_factors(x1)])+1
    # print(tmp)
    if tmp>=3:
        ans = min(ans, tmp)
    if ans < x1//2:break
print(ans)

n = int(input())
ans = n
f = [0]*(n+1)
for i in range(2, n+1):
    if f[i]==0:
        for j in range(i*2, n+1, i):
            f[j] = i
    f[i] = i-f[i]+1
ans = min(ans, min([f[i] for i in range(f[n], n+1)]))
print(ans)

n = int(input())

ans = n

f = [0]*(n+1)

for i in range(2, n+1):

    if f[i]==0:

        for j in range(i*2, n+1, i):

            f[j] = i

    f[i] = i-f[i]+1

for i in range(f[n], n+1):

    ans = min(ans, f[i])

print(ans)

# Made By Mostafa_Khaled

N = int(input())
P = [0 for _ in range(1000010)]
# store the largest prime factor of a number --> if number is prime, mark its multiples
for i in range(2, N):
    if P[i] == 0:
        for j in range(2*i, N+1, i):
            P[j] = i
ans = 1000010
for i in range(N-P[N]+1, N+1):
    ans = min(ans, i-P[i]+1)
print(ans)

N=1<<20
p=[0]*N
for i in range(2,N):
    if not p[i]:
        for j in range(i,N,i):
            p[j]=i
n=input()
x=n-1
for i in range(n-p[n]+1,n):
    if i!=p[i]:
        x=min(x,i-p[i]+1)
print x

small_prime = [0]*(10**6+1)
for i in range(2,10**6+1):
    if small_prime[i] != 0:
        continue
    small_prime[i] = i
    for j in range(1,10**6+1):
        temp = i*j
        if temp>10**6:
            break
        small_prime[temp] = i

def find(X):
    # find all prime
    org = X
    P = set([])
    while small_prime[X] != X:
        P.add(small_prime[X])
        X = X//small_prime[X]
    P.add(X)
    P = sorted(list(P))
    Mp = P[-1]
    if Mp == org:
        return X
    return org-Mp+1

def find_ans(X):
    X1 = find(X)
    ans = X
    for x1 in range(X1,X):
        ans = min(ans,find(x1))
    return ans

print(find_ans(int(input())))

p=int(input())
f=[0]*(p+1)
for i in range(2,p+1):
    if not f[i]:
        for j in range(2*i,p+1,i):
            f[j]=i
ans=p
for i in range(p-f[p]+1,p+1):
    ans=min(i-f[i]+1,ans)
print(ans)

def find_max_p(k):
    cur_k = k
    max_d = int(k**0.5)+2
    d = 2
    max_p = 1
    while cur_k > 1 and d < max_d:
        while cur_k %d == 0:
            cur_k //= d
            max_p = d

        d+=1

    if cur_k >1:
        return cur_k
    else:
        return max_p

def min_x(x):
    res = x - find_max_p(x) +1
    if res>1:
        return res
    else:
        return x

x_2 = int(input())
# print(min_x(x_2))
# for x_1 in range(min_x(x_2), x_2):
#     print(x_1, min_x(x_1))
print(min([min_x(x_1) for x_1 in range(min_x(x_2), min(x_2, min_x(x_2)+100))]))

from sys import stdin
maxn = 1000006
prime = [0] * maxn
def getPrimes():
    n = 2
    while(n < maxn):
        num = n*2
        while(num < maxn):
            prime[num] = n
            num += n
        num = n + 1
        while(  num < maxn and prime[num]!= 0 ):
            num += 1
        n = num

def main():
    getPrimes()
    n = int(stdin.readline().strip())
    a = 0
    b = prime[n]
    x0 = 9999999
    for i in range(n-b+1,n+1):
        a = prime[i]
        x0 = min(i-a+1,x0)
    print(x0)

main()

x2 = int(input())
n = x2
vis = [False for i in range(n+1)]
f = [0 for i in range(n+1)]
for i in range(2, n+1):
    if not vis[i]:
        for j in range(i+i, n+1, i):
            vis[j] = True
            f[j] = i
ans = x2
for i in range(x2 - f[x2] + 1, x2+1):
    ans = min(ans, i - f[i] + 1)
print(ans)

# just need to use only max prime factor p_max of x2, because (x2-p_max, x2] contains all possible x1

def prime_factors(n):
    f = 2
    while f * f <= n:
        while n % f == 0:
            yield f
            n //= f
        f += 1
    if n > 1:
        yield n

x2 = int(input())
max_p_x2 = max([x for x in prime_factors(x2)])
ans = float('Inf')
for x1 in range((x2 - max_p_x2 + 1), x2 + 1):
    max_p_x1 = max([x for x in prime_factors(x1)])
    if x1 - max_p_x1 > 0:
        ans = min(ans, x1 - max_p_x1 + 1)
    else:
        ans = min(ans, x1)
    if ans < x1 // 2:
        break

print(ans)

def factorize(n):
    res = []
    if n % 2 == 0:
        power = 0
        while n % 2 == 0:
            power += 1
            n //= 2
        res.append((2, power))
    i = 3
    while i * i <= n:
        if n % i == 0:
            power = 0
            while n % i == 0:
                power += 1
                n //= i
            res.append((i, power))
        i += 2
    if n > 1:
        res.append((n, 1))
    return res

x2 = int(input())

p = max(k for k, _ in factorize(x2))
k = x2 // p
res = float('inf')
for x1 in range(p * (k - 1) + 1, p * k + 1):
    p = max(k for k, _ in factorize(x1))
    k = x1 // p
    if k > 1:
        res = min(res, p * k - p + 1)
        if res < x1 // 2: break
print(res)

import sys
sys_in = sys.stdin

X2 = int(sys.stdin.read())

primes = range(X2+1)

for i in range(2, (X2+1)/2+1):
    if primes[i] == i:
        for k in range(i, X2+1, i):
            primes[k] = i

def get_max_div(i):
    return primes[i]

min_X0 = -1
d_max = get_max_div(X2)
for X1 in range(X2, X2-d_max, -1):
    p0 = get_max_div(X1)
    X0 = X1-p0+1
    if X0 == 1:
        X0 = X1
    # print "{} p0, {} X0, {} p1, {} X1".format(p0, X0, d, X1)
    if X0 < min_X0 or min_X0 == -1:
        min_X0 = X0

sys.stdout.write(str(min_X0)+"\n")

import math
def primes(max_n):
    max_n-=1
    numbers = range(3, max_n+1, 2)
    half = (max_n)//2
    initial = 4

    for step in xrange(3, max_n+1, 2):
        for i in xrange(initial, half, step):
            numbers[i-1] = 0
        initial += 2*(step+1)
        if initial > half:
            return [2] + filter(None, numbers)
def factors(a):
    j = 0
    p = {}
    while (a > 1):
        b = asd[j]
        if a % b == 0:
            a /= b
            if b not in p:
                p[b] = 1
        else:
            j += 1
    p = p.keys()
    p.sort()
    return p
asd=primes(10**6+1)
check={}
for i in asd:
    check[i]=1
def help(arr,num):
    don=[]
    if num not in check:
        for i in arr:
            p=num/i
            #print p,i
            for j in range(num-i+1,num+1):
                    don.append(j)
    return don

#print help(factors(16),16)

n=input()
ans=[]
al={}
f=factors(n)
giv=primes(n)
giv.sort(reverse=True)
m=10**7
for i in f:
    left=n-i+1
    right=n
    for j in giv:
        rem=j-left%j
        if rem==j:
            rem=0
        num=left+rem
        #print num,left,j
        if num<=right:
            ans=num-j+1
            if ans>1:
                m=min(ans,m)

print m

import sys
from math import ceil

def primes_sieve(limit):
    a = [True] * limit
    a[0] = a[1] = False
    for (i, isprime) in enumerate(a):
        if isprime:
            for n in range(i*i, limit, i):
                a[n] = False
    return a

def findLargestPrimeFactor(n):
    primeFactor = 1
    i = 0
    while primes2[n] == False:
    	if n%primes[i] == 0:
    		primeFactor = primes[i]
    		n /= primes[i]
    	else:
    		i += 1
    if primeFactor < n:
        primeFactor = n
    return primeFactor

def solution(x, step=None):
	largestPrimeFactor = findLargestPrimeFactor(x)
	if step != None:
		if largestPrimeFactor == x:
			return x
		return x - largestPrimeFactor + 1
	else:
		x1s = [i for i in range(x-largestPrimeFactor+1,x+1)]
		min_x0 = float('inf')
		for x1 in x1s:
			min_x0 = min(min_x0, solution(x1,1))
		return min_x0

primes2 = primes_sieve(1000001)
primes = [i for i,b in enumerate(primes2) if b]
x2 = int(sys.stdin.readline().strip())
print(solution(x2))
#sauce tomate

from math import *
N = int(raw_input())
primes = [2]
for i in range (3, 10**6):
    count = 0
    j = 0
    while primes[j] <= sqrt(i) and j < len(primes):
        if (i/primes[j]) * primes[j] == i:
            count = 1
            break
        else:
            j += 1
    if count == 0:
        primes.append(i)
def maxprime(n):
    max = 0
    i = 0
    while (n>1):
        m = n/primes[i]
        if m*primes[i] == n:
            n = m
            max = primes[i]
        else:
            i += 1
    return max

def primesearch(min, max):
    low = 0
    high = len(primes)
    while high > low + 1:
        mid = (low + high)/2
        val = 2 * primes[mid]
        if val == min:
            return 2*primes[mid]
        elif val >= min:
            high = mid
        elif val < min:
            low = mid
    if (2*primes[high]) <= max:
        return 2*primes[high]
    else:
        return max

p = maxprime(N)
ans = float("inf")

for i in range (N - p + 1, primesearch(N-p+1, N-1)+1):
    j = maxprime(i)
    if i==j:
        ans = min(ans, i)
    else:
        ans = min(ans, i-j+1)

print ans

n=input();ans=999999999

def isPrime(n):
  for i in xrange(2,int(n**0.5)+1):
    if n%i==0:
      return False
  return True

for i in xrange(n/2,0,-1):
  if n%i==0 and isPrime(i):
    break

for j in xrange(n-i+1,n):
  for k in xrange(j/2+1,0,-1):
    if j%k==0 and isPrime(k):
      break
  if (j-k+1)<ans:
    ans=(j-k+1)
  if j/2>ans:
    break
print ans

from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
	return ''.join(random.choice(chars) for _ in range(size))

def mp():
	return map(int,str(raw_input()).split())

sieve=[1 for i in range(10**6+1)]
sieve[1]=0
sieve[0]=0
primes=[[] for i in range(10**6+1)]
for i in range(2,10**6+1):
	if sieve[i]==1:
		for j in range(i*2,10**6+1,i):
			sieve[j]=i
			primes[j]+=[i]

#print sieve[:10]
x=input()
l=primes[x]
ans=10**18
for i in range(len(l)):
	for j in range(x-l[i]+1,x+1):
		ans=min(ans,(j)-sieve[j]+1)

print ans

x=int(input())
prim=[0]*(x+1)
for n in range(2,x+1):
    if not prim[n]:
        #prim[n]=n
        for k in range(2*n,x+1,n):
            prim[k]=n

p=prim[x]
res=1e9
for n in range(x-p+1,x+1):
    cur=n
    mxp=prim[cur]
    d=cur-mxp+1
    res=min(res,d)

print(res)

import math,sys,bisect,heapq
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(200000000)
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
ilelec = lambda: map(int1,input().split())
alelec = lambda: list(map(int1, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c):  print(["NO","YES"][c])
def y(c):  print(["no","yes"][c])
def Yy(c):  print(["No","Yes"][c])

N = int(input())
P = [0 for _ in range(1000010)]
for i in range(2, N):
    if P[i] == 0:
        for j in range(2*i, N+1, i):
            P[j] = i
ans = 1000010
for i in range(N-P[N]+1, N+1):
    ans = min(ans, i-P[i]+1)
print(ans)

from math import sqrt
import sys

# from io import StringIO
#
# sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read())

def largest_prime_factor(n):
    for i in range(2, int(sqrt(n) + 1)):
        if n % i == 0:
            return largest_prime_factor(n // i)
    return n

x2 = int(input())
p2 = largest_prime_factor(x2)

m = sys.maxsize
for x1 in range(x2 - p2 + 1, x2 + 1):
    p1 = largest_prime_factor(x1)
    if p1 != x1 and m > x1 - p1 + 1:
        m = x1 - p1 + 1

print(m)

x=int(input())
p=[1]*(x+1)
a=2
p[0]=0
p[1]=0
while a<=x:
    if p[a]:
        for i in range(a**2,x+1,a):
            p[i]=0
    a+=1
c=[]
for i in range(2,len(p)-1):
    if p[i]:
        c.append(i)
for i in range(len(c)-1,-1,-1):
    if not x%c[i]:
        low=c[i]
        break
poss=[i for i in range(max(x-low+1,3),x)]
ans=poss[0]
l=0
r=1
try:
    while poss[0]>=c[r+1]*2:
        r+=1
except IndexError:
    ...
for i in poss:
    while i-c[l]+1>=ans:
        l+=1
    if c[r]*2<=i:
        r+=1
    if l>r:
        break
    for j in range(l,r+1):
        if not i%c[j] and i-c[j]>1:
            ans=min(ans,i-c[j]+1)
print(ans)

n = int(input())
e = [0 for i in range(n+1)]
for i in range(2, n+1):
  if (e[i] == 0):
    for j in range(i+i, n+1, i):
      e[j] = i
  e[i] = i -e[i]+1
ans = n
for i in range(e[n], n+1):
  ans = ans if (ans<e[i]) else e[i]
print(ans)

x2 = int(input())

s = [-1] * (x2 + 1)
for i in range(2, x2+1):
	if s[i] == -1:
		for j in range(i, x2+1, i):
			s[j] = i

ans = x2
for x1 in range(x2 - s[x2] + 1, x2 + 1):
	x0 = x1 - s[x1] + 1
	if x0 > 1:
		ans = min(ans, x0)

print(ans)

x = int(input())
arr = [0] * (x+1)
res=x
for i in range(2, x+1):
    if arr[i]==0:
        for j in range(i+i, x+1, i):
            arr[j] = i
    arr[i]=i-arr[i]+1;
for i in range(arr[x],x+1):
    res=min(res, arr[i])
print(res)

x = int(input())

arr = [0 for i in range(x + 1)]

for i in range(2, x+1):
    if(arr[i] == 0):
        for j in range(i + i, x + 1, i):
            arr[j] = i
    arr[i] = i - arr[i] + 1;

result = x
for i in range(arr[x], x + 1):
    result = min(result, arr[i])

print(result)

from math import floor, sqrt
import bisect

import math

def rwh_primes2(n):
    # https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    correction = (n%6>1)
    n = {0:n,1:n-1,2:n+4,3:n+3,4:n+2,5:n+1}[n%6]
    sieve = [True] * (n//3)
    sieve[0] = False
    for i in range(int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      ((k*k)//3)      ::2*k]=[False]*((n//6-(k*k)//6-1)//k+1)
        sieve[(k*k+4*k-2*k*(i&1))//3::2*k]=[False]*((n//6-(k*k+4*k-2*k*(i&1))//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

k = int(input())

primes = rwh_primes2(k)

a = 1
p2 = 2
for i in primes[::-1]:
    if k%i == 0:
        p2 = i
        break

xx = range(k-p2+1, k+1)
#print(list(xx))
if p2>240:
    p1 = primes[bisect.bisect_left(primes, int(math.ceil(xx[0]/2)))]
    print(p1+1)
else:
    ans = k
    p1 = 1
    for x1 in xx:
        for i in primes[::-1]:

            if i >= x1:
                continue

            if x1 % i == 0:
                p1 = i
                break
        ans = min(ans, x1-p1+1)

    print(ans)

def testPrimal(n):
    siv = [0 for _ in range(n+1)]
    for i in range(2, n+1):
        if siv[i] == 0:
            for j in range(i+i, n+1, i):
                siv[j] = i
        siv[i] = i - siv[i] + 1
    result = n
    for i in range(siv[n], n + 1):
        result = min(result, siv[i])
    return result
n = int(input())
print(testPrimal(n))

u=[]
z=float("inf")
for x in range(1,1000009):
	u.append(False)

n=int(input())
for x in range(2,n):
	if(not u[x]):
		for y in range(2*x,n+1,x):
			u[y]=x

for x in range(n-u[n]+1,n+1):
	z=min(z,x-u[x]+1)
print(z)

a = int(input())
b = [0 for i in range(a+1)]
for i in range(2, a+1):
  if (b[i] == 0):
    for j in range(i+i, a+1, i):
      b[j] = i
  b[i] = i -b[i]+1
prim = a
for i in range(b[a], a+1):
  prim = prim if (prim<b[i]) else b[i]
print(prim)

number=int(input())
esPrimo=[True]*number
for i in range(2,number):
    if esPrimo[i]:
        for j in range (i*i,number,i):
            esPrimo[j]=False
primos=[i for i in range(2,number) if esPrimo[i]]
m=number -1
for p in primos:
    if number % p == 0:
        m=number-p
answer = m+1
for p in primos:
    if p>m:
        break
    d=m-m%p
    if d+p<=number:
        answer=min(answer,d+1)
print(answer)

lectura = int(input())
lista0 = [0]*(lectura+1)
for i in range(2, lectura+1):
    #print("i= "+str(i))
    if lista0[i]==0:
        for j in range(2*i, lectura+1, i):
            #print("j= "+str(j))
            lista0[j] = i
    lista0[i] = i-lista0[i]+1
conclusion = lectura
for i in range(lista0[lectura], lectura+1):
    conclusion = min(conclusion, lista0[i])
print(conclusion)

numero = int(input())

lista = [0]*(numero+1)

for i in range(2, numero+1):
    if lista[i]==0:
        for j in range(2*i, numero+1, i):
            lista[j] = i

    lista[i] = i-lista[i]+1

p = numero

for i in range(lista[numero], numero+1):

    p = min(p, lista[i])

print(p)

# By Sieve of Erastoteles
def getPrimes(n):
	primes = [0 for _ in range(n + 1)] # Initialize 'primes' in 0
	for i in range(2, n + 1): # n + 1 is the last we will need
	    if not primes[i]: # if it is zero, apply algorithm
	        for j in range(2*i, n + 1, i):
	            primes[j] = i
	    primes[i] = i - primes[i] + 1; # Game
	# print(primes)
	return primes

x2 = int(input())
primes = getPrimes(x2)

res = x2
for i in range(primes[x2], x2 + 1):
	res = min(res, primes[i])

print(res)

n = int(input())

ans = n

f = [0]*(n+1)

for i in range(2, n+1):

    if f[i]==0:

        for j in range(i*2, n+1, i):

            f[j] = i

    f[i] = i-f[i]+1

for i in range(f[n], n+1):

    ans = min(ans, f[i])

print(ans)

# Made By Mostafa_Khaled

if __name__ == '__main__':
    x = int(input())
    pSieve = [True] * x

    for i in range(2, x):
        if pSieve[i]:
            for j in range(i * i, x, i):
                pSieve[j] = False

    primos = []
    for i in range(2,x):
        if pSieve[i]:
            primos.append(i)

    y = x-1

    for p in primos:
        if x % p == 0:
            y = x - p

    z = y + 1

    for i in primos:
        if i > y:
            break
        d = y - y % i
        if d + i <= x:
            z = min(z, d+1)

    print(z)

x2 = int(input())

n = 1000001
max_prime_div = [0] * n
sieve = list(range(n))
sieve[1] = 0
for i in sieve:
    if sieve[i]:
        for j in range(2 * i, n, i):
            sieve[j] = 0
            max_prime_div[j] = i

min_x0 = n
for x in range(x2 - max_prime_div[x2] + 1, x2 + 1):
    max_div = max_prime_div[x]
    tmp = x - max_div + 1
    if max_div and tmp < min_x0:
        min_x0 = tmp
    if max_div and x // max_div == 2:
        break
print(min_x0)

x2 = int(input())

n = 1000001
max_prime_div = [0] * n
sieve = list(range(n))
sieve[1] = 0
for i in sieve:
    if i > 1 and sieve[i]:
        for j in range(2 * i, n, i):
            sieve[j] = 0
            max_prime_div[j] = i

min_x0 = n
for x in range(x2 - max_prime_div[x2] + 1, x2 + 1):
    max_div = max_prime_div[x]
    tmp = x - max_div + 1
    if max_div and tmp < min_x0:
        min_x0 = tmp
print(min_x0)

import array

x2 = int(input())

n = 1000001
max_prime_div = array.array('i', [0] * n)
sieve = array.array('i', list(range(n)))
sieve[1] = 0
for i in sieve:
    if sieve[i]:
        for j in range(2 * i, n, i):
            sieve[j] = 0
            max_prime_div[j] = i

min_x0 = n
for x in range(x2 - max_prime_div[x2] + 1, x2 + 1):
    max_div = max_prime_div[x]
    tmp = x - max_div + 1
    if max_div and tmp < min_x0:
        min_x0 = tmp
    if max_div and x // max_div == 2:
        break
print(min_x0)

def max_less_prime_divisor(n): # 1 for primes
    d, max_d = 2, 1
    while d * d <= n:
        while n % d == 0:
            max_d = d
            n //= d
        d += 1
    return n if n != 1 and max_d != 1 else max_d

n = int(input())
m = n - max_less_prime_divisor(n) + 1
answer = m
is_prime = [True] * m
for i in range(2, m):
    if is_prime[i]:
        d = (m - 1) - (m - 1) % i
        if d + i <= n:
            answer = min(answer, d + 1)
        for j in range(i * i, m, i):
            is_prime[j] = False
print(answer)

n = int(input())

is_prime = [True] * n
for i in range(2, n):
    if is_prime[i]:
        for j in range(i * i, n, i):
            is_prime[j] = False
primes = [i for i in range(2, n) if is_prime[i]]

m = n - 1
for p in primes:
    if n % p == 0:
        m = n - p

answer = m + 1
for p in primes:
    if p > m:
        break
    d = m - m % p
    if d + p <= n:
        answer = min(answer, d + 1)
print(answer)

x2 = int(input())
s = [-1] * (x2+1)
for i in range(2, x2+1):
    if s[i] == -1:
        for j in range(i, x2+1, i):
            s[j] = i;
ans = x2
for x1 in range(x2-s[x2]+1, x2+1):
    x0 = x1 - s[x1] + 1
    if x0 > 1:
        ans = min(ans, x0)
print(ans)

n = int(input())

siv = [0 for _ in range(n+1)]

for i in range(2, n+1):
    if siv[i] == 0:
        for j in range(i+i, n+1, i):
            siv[j] = i
    siv[i] = i - siv[i] + 1;

result = n
for i in range(siv[n], n + 1):
    result = min(result, siv[i])

print(result)

#!/bin/python
from __future__ import print_function
import sys
import math

it = iter(sys.stdin.read().splitlines())
x2 = int(next(it))

sieve = [True] * x2

for i in range(2, x2):
    if sieve[i]:
        for j in range(i * i, x2, i):
            sieve[j] = False
primes = []

for i in xrange(2,x2):
    if sieve[i]:
        primes.append(i)

x1 = x2 - 1

for p in primes:
    if x2 % p == 0:
        x1 = x2 - p
x0 = x1 + 1

for i in primes:
    if i > x1:
        break
    d = x1 - x1 % i
    if d + i <= x2:
        x0 = min(x0, d + 1)

print(x0)

from __future__ import print_function
import sys
it = iter(sys.stdin.read().splitlines())

x2 = int(next(it))

pSieve = [True] * x2

for i in range(2, x2):
    if pSieve[i]:
        for j in range(i * i, x2, i):
            pSieve[j] = False

primos = []
for i in xrange(2,x2):
    if pSieve[i]:
        primos.append(i)

x1 = x2-1

for p in primos:
    if x2 % p == 0:
        x1 = x2 - p

x0 = x1 + 1

for i in primos:
    if i > x1:
        break
    d = x1 - x1 % i
    if d + i <= x2:
        x0 = min(x0, d+1)

print(x0)

def minimum(a, b):
    if a < b:
        return a
    return b

def main():
    n = input()
    resultado = n
    listofzeros = [0] * 1000010
    for i in range(2, n + 1):
        if(listofzeros[i] == 0):
            for j in range(2 * i, n + 1, i):
                listofzeros[j] = i
        listofzeros[i] = i - listofzeros[i] + 1
    for i in range(listofzeros[n], n + 1):
        resultado = minimum(resultado, listofzeros[i])
    print resultado

main()

from __future__ import print_function, division
import sys

x = int(sys.stdin.read())
primo = [0] * (x+1)

for i in xrange(2, x+1):
    if(primo[i]==0):
        for j in xrange(i*2, x+1, i):
            primo[j] = i
    primo[i] = i - primo[i] + 1

prime = x

for i in xrange(primo[x], x+1):
    prime = min(prime, primo[i])

print(prime)

from __future__ import print_function
import sys

it = iter(sys.stdin.read().splitlines())
n = int(next(it))

lista = [0]*(n+1)
for i in xrange(2, n+1):
    if(lista[i] == 0):
        for k in xrange(2*i, n+1, i):
            lista[k] = i
    lista[i] = i - lista[i] + 1
pr = n
for i in xrange(lista[n], n+1):
    pr = min(pr, lista[i])

print(pr)

from math import sqrt
N = 10 ** 6 + 1
qis_prime = [True] * (N + 1)
for d in range(2, int(sqrt(N + 1)) + 1):
    for comp in range(2 * d, N + 1, d):
        qis_prime[comp] = False

def is_prime(n):
    return qis_prime[n]

def kryak(x2):
    ans = 0
    for i in range(1, int(x2 ** 0.5) + 1):
        if x2 % i == 0:
            if is_prime(x2 // i):
                return x2 // i
            if is_prime(i):
                ans = max(ans, i)
    return ans

x2 = int(input())
p = kryak(x2)
ans = x2
for x1 in range(x2 - p + 1, x2):
    if not is_prime(x1):
        pp = kryak(x1)
        ans = min(ans, x1 - pp + 1)
    else:
        ans = min(ans, x1)
print(ans)

#Code by Sounak, IIESTS
#------------------------------warmup----------------------------

import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#-------------------game starts now-----------------------------------------------------
def lpf(n,m=0,i=2):
    while i*i<=n:
        if n%i:i+=1
        else:n//=i
    return m-n
x=int(input())
a=x
for i in range(lpf(x,x+1),x+1):
    if lpf(i,i+1)>=3:a=min(lpf(i,i+1),a)
print(a)

x = int(input())
n = x
vis = [False for i in range(n+1)]
flag = [0 for i in range(n+1)]
for i in range(2, n+1):
    if not vis[i]:
        for j in range(i+i, n+1, i):
            vis[j] = True
            flag[j] = i
ans = x
for i in range(x - flag[x] + 1, x+1):
    ans = min(ans, i - flag[i] + 1)
print(ans)

def gf(n):
    d = 2
    while d * d <= n:
        f = 1
        while n % d is 0:
            if f:
                yield d
            f = 0
            n //= d
        d += 1
    if n > 1:
        yield n
x2 = int(input())
print(min(x1 - p + 1 for x1 in range(x2 - max(gf(x2)) + 1, x2 + 1) for p in gf(x1) if p != x1))

x2 = int(input())

def maxPrimeDevisor(v):
    result = 0
    while v % 2 == 0:
        result = 2
        v //= 2
    i = 3
    while i * i <= v:
        if v % i == 0:
            while v % i == 0:
                v //= i
            result = max(result, i)
        else:
            i += 2
    if v > 1:
        result = max(result, v)
    return result

x1 = [max(3, x2 - maxPrimeDevisor(x2) + 1), x2]
x0 = x1[1]
for i in range(x1[0], x1[1] + 1):
    mpd = maxPrimeDevisor(i)
    if mpd != i:
        x0 = max(3, min(x0, i - maxPrimeDevisor(i) + 1))
print(x0)

def lpf(n,m=0,i=2):
    while i*i<=n:
        if n%i:i+=1
        else:n//=i
    return m-n
x=int(input())
a=x
for i in range(lpf(x,x+1),x+1):
    if lpf(i,i+1)>=3:a=min(lpf(i,i+1),a)
print(a)

N = int(1e+6 + 1)
x2 = int(input())
x0 = int(1e+9)

was = [False] * N
p = [0] * N

for i in range(2, N):
    if was[i]:
        continue
    p[i] = i
    j = 2 * i
    while j < N:
        was[j] = True
        p[j] = max(p[j], i)
        j += i

for i in range(x2 - p[x2] + 1, x2 + 1):
    if p[i] == i:
        continue
    x0 = min(x0, i - p[i] + 1)

print(x0)

#---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq,bisect
import sys
from collections import deque,defaultdict
from fractions import Fraction
mod=10**9+7
mod1=998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class SegmentTree1:
    def __init__(self, data, default=9999999, func=lambda a, b: min(a , b)):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        if start == stop:
            return self.__getitem__(start)
        stop += 1
        start += self._size
        stop += self._size

        res = self._default
        while start < stop:
            if start & 1:
                res = self._func(res, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res = self._func(res, self.data[stop])
            start >>= 1
            stop >>= 1
        return res

    def __repr__(self):
        return "SegmentTree({0})".format(self.data)

# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
    def __init__(self, data, default=0, func=lambda a, b: a + b):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        if start == stop:
            return self.__getitem__(start)
        stop += 1
        start += self._size
        stop += self._size

        res = self._default
        while start < stop:
            if start & 1:
                res = self._func(res, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res = self._func(res, self.data[stop])
            start >>= 1
            stop >>= 1
        return res

    def __repr__(self):
        return "SegmentTree({0})".format(self.data)
#-------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
    def __init__(self, MOD):
        self.MOD = MOD
        self.factorials = [1, 1]
        self.invModulos = [0, 1]
        self.invFactorial_ = [1, 1]

    def calc(self, n):
        if n <= -1:
            print("Invalid argument to calculate n!")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        if n < len(self.factorials):
            return self.factorials[n]
        nextArr = [0] * (n + 1 - len(self.factorials))
        initialI = len(self.factorials)
        prev = self.factorials[-1]
        m = self.MOD
        for i in range(initialI, n + 1):
            prev = nextArr[i - initialI] = prev * i % m
        self.factorials += nextArr
        return self.factorials[n]

    def inv(self, n):
        if n <= -1:
            print("Invalid argument to calculate n^(-1)")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        p = self.MOD
        pi = n % p
        if pi < len(self.invModulos):
            return self.invModulos[pi]
        nextArr = [0] * (n + 1 - len(self.invModulos))
        initialI = len(self.invModulos)
        for i in range(initialI, min(p, n + 1)):
            next = -self.invModulos[p % i] * (p // i) % p
            self.invModulos.append(next)
        return self.invModulos[pi]

    def invFactorial(self, n):
        if n <= -1:
            print("Invalid argument to calculate (n^(-1))!")
            print("n must be non-negative value. But the argument was " + str(n))
            exit()
        if n < len(self.invFactorial_):
            return self.invFactorial_[n]
        self.inv(n)  # To make sure already calculated n^-1
        nextArr = [0] * (n + 1 - len(self.invFactorial_))
        initialI = len(self.invFactorial_)
        prev = self.invFactorial_[-1]
        p = self.MOD
        for i in range(initialI, n + 1):
            prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
        self.invFactorial_ += nextArr
        return self.invFactorial_[n]

class Combination:
    def __init__(self, MOD):
        self.MOD = MOD
        self.factorial = Factorial(MOD)

    def ncr(self, n, k):
        if k < 0 or n < k:
            return 0
        k = min(k, n - k)
        f = self.factorial
        return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
#--------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
    if a == 1 or n == 0:
        return 1
    if n % 2 == 0:
        s = powm(a, n // 2, m)
        return s * s % m
    else:
        return a * powm(a, n - 1, m) % m
#--------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
    zipped_pairs = zip(list2, list1)

    z = [x for _, x in sorted(zipped_pairs)]

    return z
#--------------------------------------------------product----------------------------------------
def product(l):
    por=1
    for i in range(len(l)):
        por*=l[i]
    return por
#--------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
    left = 0
    right = n - 1

    count = 0

    while (left <= right):
        mid = int((right + left) / 2)

        # Check if middle element is
        # less than or equal to key
        if (arr[mid] <= key):

            # At least (mid + 1) elements are there
            # whose values are less than
            # or equal to key
            count = mid + 1
            left = mid + 1

        # If key is smaller, ignore right half
        else:
            right = mid - 1

    return count
#--------------------------------------------------binary----------------------------------------
def countdig(n):
    c=0
    while(n>0):
        n//=10
        c+=1
    return c
def countGreater(arr, n, k):
    l = 0
    r = n - 1

    # Stores the index of the left most element
    # from the array which is greater than k
    leftGreater = n

    # Finds number of elements greater than k
    while (l <= r):
        m = int(l + (r - l) / 2)

        # If mid element is greater than
        # k update leftGreater and r
        if (arr[m] > k):
            leftGreater = m
            r = m - 1

        # If mid element is less than
        # or equal to k update l
        else:
            l = m + 1

    # Return the count of elements
    # greater than k
    return (n - leftGreater)
#--------------------------------------------------binary------------------------------------
def SieveOfEratosthenes(n):
    ans=[]
    prime = [-9999999999 for i in range(n + 1)]
    p = 2
    while (p<= n):
        if (prime[p] ==-9999999999 ):
            for i in range(p * 2, n + 1, p):
                prime[i] = p
        p += 1
    prime=[i-prime[i]+1 for i in range(n+1)]
    #print(prime)
    return (prime)
#---------------------------------------0(n)---------------------------------------------------
n=int(input())
prime=SieveOfEratosthenes(n)
ans=min(prime[prime[n]:n])
if ans>10**6:
    ans-=9999999999+1
print(ans)

x2 = int(input())
n = x2
vis = [False for i in range(n+1)]
f = [0 for i in range(n+1)]
for i in range(2, n+1):
    if not vis[i]:
        for j in range(i+i, n+1, i):
            vis[j] = True
            f[j] = i
ans = x2
for i in range(x2 - f[x2] + 1, x2):
    ans = min(ans, i - f[i] + 1)
print(ans)

import sys
from math import ceil

def primes_sieve(limit):
    a = [True] * limit
    a[0] = a[1] = False

    for (i, isprime) in enumerate(a):
        if isprime:
            for n in range(i*i, limit, i):
                a[n] = False
    return a

def solution(x2):
	x1s = set([])
	for p in primes:
		if x2%p:
			continue
		for x1 in range(x2-p+1,x2):
			x1s.add(x1)
	x1s = list(x1s)
	min_x0 = float('inf')
	for x1 in x1s[10000:]:
		for p in primes:
			if x1%p:
				continue
			if x1 == p:
				if x1 < 3:
					continue
				min_x0 = min(min_x0, x1)
			else:
				if x1-p+1 < 3:
					continue
				min_x0 = min(min_x0, x1-p+1)
	return min_x0

primes = primes_sieve(1000001)
x2 = int(sys.stdin.readline().strip())
primes = [i for i,p in enumerate(primes) if p == True and i <= x2+1]
print(solution(x2))

import sys
from math import ceil

def primes_sieve(limit):
    a = [True] * limit
    a[0] = a[1] = False

    for (i, isprime) in enumerate(a):
        if isprime:
            for n in range(i*i, limit, i):
                a[n] = False
    return a

def solution(x2):
	x1s = set([])
	for p in primes:
		if x2%p:
			continue
		for x1 in range(x2-p+1,x2):
			x1s.add(x1)
	min_x0 = float('inf')
	for x1 in sorted(x1s):
		for p in primes:
			if x1-p+1 >= min_x0:
				break
			if x1%p:
				continue
			if x1 == p:
				if x1 < 3:
					continue
				min_x0 = min(min_x0, x1)
			else:
				if x1-p+1 < 3:
					continue
				min_x0 = min(min_x0, x1-p+1)
	return min_x0

primes = primes_sieve(1000001)
x2 = int(sys.stdin.readline().strip())
primes = [i for i,p in enumerate(primes) if p == True and i < x2]
print(solution(x2))

import sys
from math import ceil

def primes_sieve(limit):
    a = [True] * limit
    a[0] = a[1] = False

    for (i, isprime) in enumerate(a):
        if isprime:
            for n in range(i*i, limit, i):
                a[n] = False
    return a

def solution(x2):
	x1s = set([])
	for p in primes:
		if x2%p:
			continue
		for x1 in range(x2-p+1,x2):
			x1s.add(x1)
	x1s = list(x1s)
	min_x0 = float('inf')
	for x1 in x1s[1000:]:
		for p in primes:
			if x1%p:
				continue
			if x1 == p:
				if x1 < 3:
					continue
				min_x0 = min(min_x0, x1)
			else:
				if x1-p+1 < 3:
					continue
				min_x0 = min(min_x0, x1-p+1)
	return min_x0

primes = primes_sieve(1000001)
x2 = int(sys.stdin.readline().strip())
primes = [i for i,p in enumerate(primes) if p == True and i <= x2+1]
print(solution(x2))

n=input();dic={4:3,6:3,8:7,14:6}
def isPrime(n):
  for i in xrange(2,int(n**0.5)+1):
    if n%i==0:
      return False
  return True

def fn(n,isX0): #rets the smallest number n can reduce to
  ans=n
  for p in xrange(2,n/2+1):
    if n%p==0 and isPrime(p):
      for i in xrange(p*(n/p-1)+1,n):
        if not (not isX0 and isPrime(i)):
          ans=min(i,ans)
          break
  return ans
if n in dic:
  print dic[n]
else:
  print fn(fn(n,False),True)

from math import sqrt
import sys

# from io import StringIO
#
# sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read())

def largest_prime_factor(n):
    for i in range(2, int(sqrt(n) + 2)):
        if n % i == 0:
            return largest_prime_factor(n // i)
    return n

x2 = int(input())
p2 = largest_prime_factor(x2)
lo2 = x2 - p2 + 1

m = sys.maxsize
for x1 in range(lo2, x2 + 1):
    p1 = largest_prime_factor(x1)
    if p1 != x1 and m > x1 - p1 + 1:
        m = x1 - p1 + 1

print(m)

x=int(input())
p=[1]*(x+1)
a=2
p[0]=0
p[1]=0
while a<=x:
    if p[a]:
        for i in range(a**2,x+1,a):
            p[i]=0
    a+=1
c=[]
for i in range(2,len(p)-1):
    if p[i]:
        c.append(i)
for i in range(len(c)-1,-1,-1):
    if not x%c[i]:
        low=c[i]
        break
poss=[i for i in range(max(x-low+1,3),x)]
ans=poss[0]
l=0
r=1
try:
    while poss[0]>=c[r+1]*2:
        r+=1
except IndexError:
    ...
for i in poss:
    while i-c[l]+1>=ans:
        l+=1
    if c[r]*2<=i:
        r+=1
    if l>r:
        break
    for j in range(l,r+1):
        if not i%c[j] and i-c[j]>2:
            ans=min(ans,i-c[j]+1)
print(ans)

if __name__ == '__main__':
    x = int(input())
    pSieve = [True] * x

    for i in range(2, x):
        if pSieve[i]:
            for j in range(i * i, x, i):
                pSieve[j] = False

    primos = []
    for i in range(2,x):
        if pSieve[i]:
            primos.append(i)

    y = x-1

    for p in primos:
        if x % p == 0:
            y = x - p

    z = y + 1

    for i in primos:
        if i > y:
            break
        d = y - y % i
        if d + i <= x:
            z = min(z, d+1)

    print(d)

if __name__ == '__main__':
    x = int(input())
    pSieve = [True] * x

    for i in range(2, x):
        if pSieve[i]:
            for j in range(i * i, x, i):
                pSieve[j] = False

    primos = []
    for i in range(2,x):
        if pSieve[i]:
            primos.append(i)

    y = x-1

    for p in primos:
        if x % p == 0:
            y = x - p

    z = y + 1

    for i in primos:
        if i > y:
            break
        d = y - y % i
        if d + i <= x:
            z = min(z, d+1)

    print(i)

x2 = int(input())

def max_div(r):
    pdelim = 2
    while r > 1:
        if r % pdelim == 0:
            r = r / pdelim
        else:
            pdelim += 1
    return pdelim

min_x0 = x2
max_div_x2 = max_div(x2)
for x1 in range(max_div_x2 * (x2 // max_div_x2 - 1) + 1, x2 + 1, 1):
    if x1 == x2:
        max_x1_div = max_div_x2
    else:
        max_x1_div = max_div(x1)
    if max_x1_div != x1:
        tmp = max_x1_div * (x1 // max_x1_div - 1) + 1
        print(tmp)
        if tmp < min_x0:
            min_x0 = tmp
print(min_x0)

import sys
it = iter(sys.stdin.read().splitlines())

x2 = int(next(it))

pSieve = [True] * x2

for i in range(2, x2):
    if pSieve[i]:
        for j in range(i * i, x2, i):
            pSieve[j] = False

primos = []
for i in xrange(2,x2):
    if pSieve[i]:
        primos.append(i)

x1 = x2-1

for p in primos:
    if x2 % p == 0:
        x1 = x2 - p

x0 = x1 + 1

for i in primos:
    if i > x1:
        break
    d = x1 - x1 % i
    if d + 1 <= x2:
        x0 = min(x0, d+1)

print x0

from __future__ import print_function, division
import sys

x = int(sys.stdin.read())
primo = [True] * x

#indice te dice si es primo o no
for i in xrange(2, x):
    if(primo[i]==True):
        for j in xrange(i*i, x, i):
            primo[j] = False

listaPrimos = [i for i in xrange(2, x) if primo[i]==True]

y = x-1
for i in listaPrimos:
    if x % i == 0:
        y = x - i + 1

respuesta = y

for i in listaPrimos:
    if i > y:
        break
    z = y - (y%i)
    if z + i <= x:
        respuesta = min(respuesta, z+1)

print(respuesta)

from __future__ import print_function, division
import sys

x = int(sys.stdin.read())
primo = [True] * x

#indice te dice si es primo o no
for i in xrange(2, x):
    if(primo[i]==True):
        for j in xrange(i*i, x, i):
            primo[j] = False

listaPrimos = [i for i in xrange(2, x) if primo[i]==True]

respuesta = x-1
for i in listaPrimos:
    if x % i == 0:
        respuesta = x - i + 1

for i in listaPrimos:
    if i > respuesta:
        break
    z = respuesta - (respuesta%i)
    if z + i <= x:
        respuesta = min(respuesta, z+1)

print(respuesta)