beecrowd | 2514

Lunar Alignment

By Ricardo Oliveira, UFPR BR Brazil

Timelimit: 1

In a galaxy far, far away, there is the planet Nlôguérrà, predominantly inhabited by dinosaurs. There are three moons orbiting Nlôguérrà. The orbit of each moon has the form of a circumference whose center is Nlôguérrà, as indicated by the figure in the left:

When all three moons align between the planet and the sun, as indicated by the figure in the right, a horrible catastrophe happens! The last time it occurred, exactly M years ago, a big drought doomed the entire planet, reducing the number of dinosaurs by half. The first moon takes L1 years to complete an orbit around the planet; the second moon takes L2 years to complete an orbit around the planet; and the third moon takes L3 years to do so. Determine how many years will pass until the next alignment.

Consider that both the planet and the sun are stacionary.

Input

The input contains several test cases. The first line of each test case contains the integer M (1 ≤ M ≤ 109), indicating that the last alignment occurred M years ago. The second line contains three integers L1, L2 and L3 (1 ≤ L1, L2, L3 ≤ 103), the time it takes, in years, for each moon to complete its orbit around the planet. It is guaranteed that no alignment occurred in the last M-1 years, and no alignment occurs this year.

The input ends with end-of-file (EOF).

Output

For each test case, print a line containing a number X indicating that the next alignment between the planet and the sun will occur in X years from this day.

Input Sample Output Sample

2
1 2 3
3
2 4 8

4
5