eolymp
bolt
Try our new interface for solving problems
Problems

Biorhythms

Biorhythms

Some people believe that every human life is three cycles, originating from the moment of his birth. These cycles - cycles of physical, emotional and intellectual activity, with periods of \textbf{23}, \textbf{28} and \textbf{33} days respectively. During this period, there is only one peak, in which a person is most active in their physical, emotional or mental activity. Since all the cycles have different periods, and their peaks also fall on different days. Certain psychic wants to learn how to quickly determine at what the next day for some people will fall all the three peaks at the same time. For each cycle is known from the beginning of the day of the year in which there was a peak corresponding to the activity (not necessarily the first) to humans. Given the number of the day from the beginning, when this man came to a psychic. Your task - to determine, after a minimum number of days from a date (not counting the day of the visit) at the same time there will be peaks of all three types of activity. If this day falls on a day visit, it is necessary to find the next day "\textit{triple activity}". \InputFile The input file recorded four numbers: \textbf{p}, \textbf{e}, \textbf{i}, \textbf{d}, separated by spaces. The numbers \textbf{p}, \textbf{e}, \textbf{i} -- a number of days from the beginning of the year in which there were peaks of physical, emotional and intellectual activity, respectively. The number of \textbf{d} - day visit to a psychic, as measured from the beginning of the year (\textbf{d} may be smaller than each of the numbers \textbf{p}, \textbf{e}, \textbf{i}). All the numbers - both positive and do not exceed \textbf{365}. It is guaranteed that the next "\textit{triple peak}" will occur not later than \textbf{30000} days on a given day of the visit. \OutputFile In the output file to print a number corresponding to the number of days for which there will be a "\textit{triple peak}" of activity.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5 20 34 325
Output example #1
19575