Red Ginseng Game (Hard)
Time limit1sMemory limit512 MB
Given a cycle of N people and two pointer distances, decide the minimum number of pointings before the two pointers collide, or report that they never meet.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Graph, Shortest path
- Solved
- No attempts yet
Problem
Eunha likes drinks, games, and drinking games. Her favorite drinking game is the "red ginseng game". people sit around a table in a circle and play it by these rules.
- Eunha points at two different people.
- The two people who were pointed at each choose one person at the table at the same time and point at that person.
- If the two chose the same person, the game ends. Otherwise, go back to rule 2.
After the contest the contestants went to a nearby bar for an after party, and Eunha led them into a red ginseng game. So many people had gathered that nobody could see who was pointing at whom, and the game stopped again and again. Eunha's friend Eunseo could not watch this any longer and proposed the "orderly red ginseng game", which changes the rules like this.
- Eunha points at two different people in order. The person pointed at first holds pointer A, and the person pointed at second holds pointer B.
- The person holding pointer A points at the one person sitting exactly seats to their left or to their right and hands that person the pointer.
- If the person who was pointed at already held pointer B, the game ends.
- The person holding pointer B points at the one person sitting exactly seats to their left or to their right and hands that person the pointer.
- If the person who was pointed at already held pointer A, the game ends. Otherwise, go back to rule 2.
Thanks to Eunseo's proposal the contestants could enjoy the game in an orderly way. Eunha then kept the game running for hours, so the exhausted contestants decided to finish the game as quickly as possible no matter whom Eunha points at and no matter how she sets the two pointing distances. Rescue them from red ginseng hell.
For convenience, assume the participants are numbered 1 to counterclockwise. That is, participant sits immediately to the left of participant , and participant sits immediately to the right. As the exception, participant sits immediately to the left of participant 1, and participant 1 sits immediately to the right of participant .
Input
The first line contains the number of participants of the orderly red ginseng game (), the number of the person Eunha pointed at first and the number of the person she pointed at second (, ), and the integers and that give the pointing distance of each pointer (), separated by spaces and given in that order.
Output
On the first line, print the smallest number of pointings needed to finish the given game as quickly as possible. If the game cannot be finished, print Evil Galazy. The two people Eunha points at in rule 1 do not count toward the number of pointings.
Hint
When , , , and , the following order finishes the game in three pointings.
- Participant 5, who holds pointer A, can point at participant 4 or participant 6. Participant 5 points at participant 4 and hands over the pointer.
- Participant 1, who holds pointer B, can point at participant 5 or participant 3. Participant 1 points at participant 3 and hands over the pointer.
- Participant 4, who holds pointer A, points at participant 3, hands over the pointer, and the game ends.