Problem I. Identical digits

Author:Антон Карабанов   Time limit:1 sec
Input file:Standard input   Memory limit:512 Mb
Output file:Standard output  

Statement

Zhenya Slavin is a first human born on Mars. He is yet a little boy and likes to look at the digital clock hanging opposite of his bed. Most of all he likes the moments when all digits on the clock become identical. In those moments he always claps joyfully.

Since the period of rotation of Mars around its axis is different from that of the Earth, Martian colonists decided that:

Current time on Mars is hM hours, mM minutes and sM seconds. After how many seconds will Zhenya clap? Time on the Martian digital clocks is displayed with leading zeroes in all positions. Extra positions are not used.

Input format

Input contains six integers, one per line: h, m and s — description of Martian day subdivision, followed by hM, mM and sM — current time.

Output format

Output a single integer — number of seconds until the moment when all digits on the clock become identical.

Constraints

0 ≤ hM < h ≤ 106

0 ≤ mM < m ≤ 106

0 ≤ sM < s ≤ 106

Notes on samples

It the first sample Martian days are no different from Earth ones (24 hours of 60 minutes of 60 seconds). Now it is 23 hours 50 minutes exactly (clock displays digits 23:50:00). After 10 minutes (or 600 seconds) the clock will display zeroes in all positions.

It the second sample Martian days contain 627 hours of 5 minutes of 777 seconds. Now it is 49 hours 3 minutes and 2 seconds (clock displays digits 049:3:002). After 61 hours 3 minutes and 109 seconds (or 239425 Martian seconds) the clock will display ones in all positions.

Sample tests

No. Standard input Standard output
1
24
60
60
23
50
0
600
2
627
5
777
49
3
2
239425

0.102s 0.016s 13