Number Game
InterviewTime limit2sMemory limit512 MB
Find the smallest positive N under 1e9 satisfying N mod P1 = X1, N mod P2 = X2, N mod P3 = X3, else print -1.
- Level
Medium4 of 10
- Topics
- Number theory, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
Junseo recently learned about the remainder operation. He found it interesting that the remainder of a positive integer divided by a positive integer is always between 0 and , so he invented a number game he plays alone.
Junseo first picks positive integers , , freely. He then picks positive integers , , so that , , and . What Junseo wants to know is the smallest positive integer that satisfies all three conditions below.
- divided by leaves remainder .
- divided by leaves remainder .
- divided by leaves remainder .
Given the , , , , , that Junseo picked, write a program that finds the smallest .
Input
Six integers , , , , , are given in that order, separated by spaces. Every number is an integer between 1 and 300.
Output
Print the smallest positive integer on one line.
If no positive integer below 1,000,000,000 satisfies the conditions, print -1.