Lanterns
To prepare for the informatics olympiad meeting, the city mayor, Mykola Petrovych, has decided to install N streetlights along the main street. Each streetlight changes its light color cyclically.
According to the technical specifications, the colors are represented by natural numbers from 1 to 30. Every minute, each streetlight changes its color to the next one in sequence. Initially, a streetlight displays color 1, then 2, then 3, and so on. Once the last color is reached, it cycles back to color 1.
However, the installer, Vasyl, did not synchronize or configure the streetlights properly, so when they are turned on, they might display different colors.
After a test run, it was determined which color each streetlight initially displays and how many colors each streetlight can cycle through.
Vasyl wants to turn off the streetlights as soon as they all display the same color. Your task is to determine how long Vasyl will have to wait for this to happen.
Input
The first line contains a natural number N – the number of streetlights, where (2 ≤ N ≤ 10).
The second line contains N natural numbers separated by spaces, indicating the initial color of each streetlight.
The third line contains N natural numbers separated by spaces, indicating the maximum number of colors each streetlight can cycle through.
Output
Output a single integer representing the number of minutes Vasyl will wait for all streetlights to display the same color, or -1 if this will never happen.