50 Shades of Prime

Xoranna got into big trouble. The prime master locked her in his secret room and told her not to get out until he returns or else… He didn't say. "What would that or else mean?" she wonders.

Nevertheless, she wants to get out, and fast. She sees the lock from the distance, it requires one number to open. The number can be huge, since the lock fits up to 15 digits! Not discouraged, she checks further and finds a small note on the ground:

"Seek as far as you can go.
From the interval I give
Two coprimes you must retrieve.
Their difference should be the greatest
Of all coprime pairs you can test.
Know this difference and you may go."

Xoranna then notices two numbers on the back of the note. Could this be the interval in the riddle? Possibly, since the first one is the smallest. However, she doesn't have the time to test all numbers until the prime master returns, so she needs your help to open the lock.


The first line contains the first number while the second line contains the second number.


A natural number that solves the riddle for the given input. If the solution numbers are A and B, then the answer is B-A+1.


  • 0 < a ≤ b < 1015
  • You only have one chance to open the lock!
  • If there are no two such numbers, please print -1.
  • Two numbers are coprime if their greatest common divisor is 1.



Sponsors Gold