The Biorhythm Synchronizer
Scientists are studying biological cycles in organisms. Each organism has a cycle with a specific period and a target phase.
Given n organisms, each with a period p[i] and target phase t[i], find the earliest time T (T ≥ 0) when ALL organisms simultaneously reach their target phases.
An organism with period p and target phase t is at its target phase at time T if: T mod p == t
If no such time exists, return -1.
Input Format
The first line contains an integer n, the number of organisms. The next n lines each contain two integers: period and target_phase.
Output Format
Print the earliest time T, or -1 if impossible.
Examples
Example 1
Example 2
Constraints
•
1 ≤ n ≤ 10
•
1 ≤ period[i] ≤ 1000
•
0 ≤ target_phase[i] < period[i]
Code
Case 1
Case 2
