#t291. 人生起伏

人生起伏

Biorhythms

Problem Background

Special for beginners, ^_^

Problem Description

Human beings are born with three physiological cycles: the physical, emotional, and intellectual cycles, whose lengths are 2323, 2828, and 3333 days respectively. Each cycle has one peak day, on which a person performs exceptionally well in that aspect. Because the three cycles have different lengths, their peaks usually do not fall on the same day. For each person, we want to know when the three peaks coincide. For each cycle we are given the number of days from the first day of the current year to a peak day (not necessarily the first peak). Your task is: given a day dd counted from the first day of the year, output the number of days from dd to the next time all three peaks coincide (excluding dd itself). For example, if the given time is 1010 and the next triple-peak is at 1212, output 22 (note this is not 33).

Input Format

One line containing four integers: p,e,ip, e, i, and dd, separated by single spaces. p,e,ip, e, i indicate the days on which the physical, emotional, and intellectual peaks occur (counted from the first day of the year). dd is the given day, which may be smaller than p,ep, e, or ii. All given days are non-negative and not greater than 365365, and the answer sought is not greater than 2125221252.

Output Format

One integer: the number of days from the given day to the next triple-peak.

Sample Input

4 5 6 7

Sample Output

16994

Constraints

Time limit: 1s, memory limit: 1024KiB for each test case.