This page is still under construction.

Parts of this page are still being built. What you see may change.

Number Game

Interview

Time limit2sMemory limit512 MB

Summary
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 NN divided by a positive integer MM is always between 0 and M−1M-1, so he invented a number game he plays alone.

Junseo first picks positive integers X1X_1, X2X_2, X3X_3 freely. He then picks positive integers P1P_1, P2P_2, P3P_3 so that P1>X1P_1 > X_1, P2>X2P_2 > X_2, and P3>X3P_3 > X_3. What Junseo wants to know is the smallest positive integer NN that satisfies all three conditions below.

  • NN divided by P1P_1 leaves remainder X1X_1.
  • NN divided by P2P_2 leaves remainder X2X_2.
  • NN divided by P3P_3 leaves remainder X3X_3.

Given the P1P_1, P2P_2, P3P_3, X1X_1, X2X_2, X3X_3 that Junseo picked, write a program that finds the smallest NN.

Input

Six integers P1P_1, P2P_2, P3P_3, X1X_1, X2X_2, X3X_3 are given in that order, separated by spaces. Every number is an integer between 1 and 300.

Output

Print the smallest positive integer NN on one line.

If no positive integer below 1,000,000,000 satisfies the conditions, print -1.

Examples3

  1. Example 1

    Input
    20 20 20 1 2 3
    
    Expected output
    -1
    
  2. Example 2

    Input
    2 3 5 1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    2 4 8 1 2 3
    
    Expected output
    -1