#C5596. Prime Sum Sequence

    ID: 49262 Type: Default 1000ms 256MiB

Prime Sum Sequence

Prime Sum Sequence

You are given two integers n and x. Consider the sequence of prime numbers: \(p_1, p_2, p_3, \ldots\), where \(p_i\) denotes the \(i\)-th prime number. Define the cumulative sum as \(S(k) = \sum_{i=1}^{k} p_i\). Your task is to determine whether there exists a positive integer \(k\) such that \(S(k) = x\) while \(S(k)\) does not exceed \(n\). If such a \(k\) exists, output the smallest possible \(k\); otherwise, output \(-1\>.

Note: The computation stops once the cumulative sum exceeds \(n\), as no further valid \(k\) can be obtained.

inputFormat

The input consists of a single line containing two space-separated integers: n and x.

\(1 \leq n, x \leq 10^9\)

outputFormat

Output a single integer representing the smallest k such that the sum of the first \(k\) prime numbers equals \(x\) and does not exceed \(n\); otherwise, output \(-1\) if no such k exists.

## sample
20 17
4

</p>