PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000101. Độ dài khoảng cách giữa hai nguyên tố (Prime Gap)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Độ dài khoảng cách giữa hai nguyên tố (Prime Gap)

Prime Gap

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Consider all prime numbers in increasing order. Two primes p<qp<q are consecutive if there is no prime strictly between them. Given an integer kk, output 00 if kk itself is prime. Otherwise, find the consecutive primes p<k<qp<k<q surrounding kk and output the gap length q−pq-p. The length is the difference between the two bounding primes, not the number of composite numbers between them.

If kk lies between consecutive primes pp and qq, the result is q−pq-p even though fewer than q−pq-p composite numbers lie between them.

Input

One line contains an integer kk.

Output

Print 00 when kk is prime; otherwise print the difference between the consecutive primes surrounding kk.

Subtasks

  • Subtask 1 (20 points): 2≤k≤5002\le k\le 500.
  • Subtask 2 (30 points): 2≤k≤1000002\le k\le 100000.
  • Subtask 3 (50 points): 2≤k≤12997092\le k\le 1299709.

Examples

Input

10

Output

4

Explanation

The consecutive primes enclosing 1010 are 77 and 1111. The numbers 8,9,108,9,10 lie between them. The prime gap length is 11−7=411-7=4, so the output is 44.