Neatness
Time limit1sMemory limit512 MB
Choose a first cleaning day among 1..k and a starting boy so that, after swap rules during non-overlapping absences, both boys clean equally often in an n-day month.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Implementation, Brute force
- Solved
- No attempts yet
Problem
Boys Dima and Mitya share a room in a dormitory and take turns cleaning it every days. A new month, consisting of days, has just begun, so the boys have to come up with a new cleaning schedule. To do it, the boys simply choose who is going to clean first, and which of the first days of the month it will happen. The next cleaning will happen exactly days after the previous one, and the other boy will have to do it. For example, if Dima cleans the room on day , then on day Mitya will have to do the cleaning, on day it will be Dima's turn again, and so on.
The schedule should be fair: both boys should clean the room an equal number of times. The situation is complicated by the fact that both Dima and Mitya are planning to go to an olympiad once during this month. If one of the boys is absent during his cleaning day, the other boy cleans in his place. The schedule does not shift in this case. It is known that Dima will be absent from the -th day to the -th day inclusive, and Mitya from the -th day until the -th day. Days in the month are numbered from 1 to . The boys' trips to olympiads do not intersect, so every day at least one of the boys is at home.
Help the boys decide who should clean the room first, and which day among the first days of the month it should happen, or determine that it is impossible.
Input
The first line contains two integers and : the number of days in this month and the number of days between two consecutive cleanings (, ).
The second line contains integers , , and , describing the boys' trips to olympiads (; ; or ).
Output
If it is impossible to create a fair schedule, print .
Otherwise, print one integer in the first line: the day of the first cleaning. On the second line print <<Dima>> if Dima should clean first, and <<Mitya>> otherwise.
If there are multiple possible solutions, print any of them.
Notes
Let us represent the samples as tables using the following notation:
- <<
d>> --- day when Dima is away, - <<
m>> --- day when Mitya is away, - <<
D>> --- day when Dima cleans the room, - <<
M>> --- day when Mitya cleans the room, - <<
*>> --- possible starting day.
Then for the first sample we get the following table:
This way both boys will clean the room an equal number of times, and the boys' trips do not affect the schedule.
Table for the second sample:
Because Mitya is absent on the third day, Dima will clean instead. Mitya will clean the room on the 15th day because of Dima's trip. This way each boy cleans the room twice, even if it was not scheduled.