Under construction

Still being built. Things may change

What the Flex?

Time limit0.5sMemory limit1024 MB

Summary
Given version a and limit N, find the next version after a that has the same set of prime divisors, or print -1 if none exists.
Level

Medium6 of 10

Topics
Number theory, Greedy, Math
Solved
No attempts yet

Problem

\textit{In this problem, you have to find the next integer in a particular ordering that has the same set of prime divisors.}

Archaeologist Kris studies the ancient platform Flex, which was recently found near Adobe creek. On this platform, the version numbers of teleporters built by The Elders were once inscribed. Each version number is an integer between 11 and NN. There is one column of numbers for each teleporter. Each column lists all created versions of that teleporter in the order they were built. Some of the numbers were erased.

Kris just finished studying version aa of teleporter bb, and he is now ready to examine the next version of this teleporter. He does not know its number, but any correct solution must find it. Here is what Kris already knows:

  1. Each number from 11 to NN is the version number of some version of some teleporter.
  2. Different versions of one teleporter have different numbers.
  3. Each teleporter has its own set of prime numbers. Consider one fixed teleporter with prime set X={p1,p2,…,pm}X = \{p_1, p_2, \dots, p_m\}, where p1<p2<⋯<pmp_1 < p_2 < \dots < p_m.
  4. Every version number of this teleporter is divisible by each prime in XX and by no prime outside XX. Such a number can be written as p1k1p2k2…pmkmp_1^{k_1} p_2^{k_2} \dots p_m^{k_m} with ki≥1k_i \ge 1. Each version maps to a tuple (k1,k2,…,km)(k_1, k_2, \dots, k_m) of mm positive integers.
  5. Version α=(k1,…,km)\alpha = (k_1, \dots, k_m) was built before version β=(l1,…,lm)\beta = (l_1, \dots, l_m) if and only if there is an ii (0≤i<m0 \le i < m) with k1=l1,…,ki=lik_1 = l_1, \dots, k_i = l_i and ki+1<li+1k_{i+1} < l_{i+1}. In other words, the versions are ordered lexicographically by their tuples.

Input

The only line of input contains two integers, the version number aa of a teleporter and the number NN (1≤a≤N≤10181 \le a \le N \le 10^{18}).

Output

If there is a version of the same teleporter that was built just after version aa, print its number. Otherwise, print "-1".

Examples2

  1. Example 1

    Input
    6 13
    
    Expected output
    12
    
  2. Example 2

    Input
    12 13
    
    Expected output
    -1