The Triple T Files
The village of Tung Tung Tung has a legendary tradition. Every morning, percussionists gather to wake the residents with a rhythmic triple-hit pattern. To ensure everyone is awake, the village elder has decreed that the music must continue until the total number of drum beats across the entire village reaches at least
.
Each percussionist has a Triple T signature defined by two integers: the period (
) and the interval (
).
A single 'Triple T' sequence for percussionist consists of exactly
drum beats played within a period of time. Say the sequence starts at time
, each of the beats will occur as follows:
- The first beat at
.
- The second beat at
.
- The third beat at
.
The percussionists are very disciplined. The -th sequence (
) for percussionist
starts exactly at time
. Therefore, the beats for percussionist
occur at:
All percussionists start their first sequence at . Your task is to find the minimum time
required such that the total number of drum beats produced by all
percussionists up to and including time
is at least
.
Input
The first line contains two integers and
,
, the number of percussionists and the total number of beats required.
Each of the next lines contains two integers
and
,
,
.
Output
Print a single integer, the minimum time such that the total number of beats that have occurred by that time is at least
.
The answer is guaranteed to be less than or equal to .
Example
Input 1
2 10
10 2
15 3
Output 1
15
For Percussionist 1 ():
- Beats occur at
(1st period)
- Beats occur at
(2nd period)
- Beats occur at
(3rd period)
For Percussionist 2 ():
- Beats occur at
(1st period)
- Beats occur at
(2nd period)
At :
- Percussionist 1 has hit 6 times.
- Percussionist 2 has hit 3 times.
- Total beats = 9 (Less than
).
At :
- Percussionist 1 has hit 6 times.
- Percussionist 2 has hit 4 times (the hit at
is the start of their 2nd period).
- Total beats = 10 (Matches
).
The minimum time is .
Input 2
5 3
100 10
100 10
100 10
100 10
100 10
Output 2
0
Input 3
1 2
10 1
Output 3
1
Comments
TRALALERO TRALALA
tung tung tung Sahur