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.

Difficulty: MEDIUM_HARD
Tags: Number theory

Mathematics

Starter code:

Solutions:
import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
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)]

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)


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)


prime_number = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009]
num = int(input())
import math

def find_max_prime(val):
	result = 0
	if val in prime_number:
		return val
	for prime in prime_number:
		if prime > math.sqrt(val):
			break
		if val % prime == 0:
			temp = max(prime, find_max_prime(int(val / prime)))
			result = max(result, temp)
			break
	if result == 0:
		return val
	else:
		return result
prev = find_max_prime(num)
val = 10000000
for i in range(num - prev + 1, num + 1):
	if val < i / 2 + 1:
		continue
	k = find_max_prime(i)
	if k == i:
		continue
	else:
		val = min(val, i - k + 1)
print(val)


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)


TN = 1

def resheto(l, k, n):
	i = 0
	while i < len(l):
		if l[i] % k == 0:
			l = l[:i] + l[i + 1:]
		i += 1
	lst = []
	for i in l:
		if n % i == 0:
			lst.append(i)
	return lst

def solution():
	x2 = int(input())
	n = 1000001
	max_prime = [0] * n
	s = list(range(n))
	s[1] = 0
	for i in s:
		if i > 1 and s[i]:
			for j in range(2 * i, n, i):
				s[j] = 0
				max_prime[j] = i
	min_x0 = n
	for x in range(x2 - max_prime[x2] + 1, x2 + 1):
		max_div = max_prime[x]
		tmp = x - max_div + 1
		if max_div and tmp < min_x0:
			min_x0 = tmp
	print(min_x0)
while TN != 0:
	solution()
	TN -= 1


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())
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)


from math import sqrt
import sys

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)