You hate waiting. You are on your way to visit a relative in New Orleans, and the bus station you are standing in has the worst schedule you have ever seen. There are no arrival times and no departure times, only a list of how long each running bus takes to finish one route. So you pull out your laptop and write a program that tells you how long you have to wait for the next bus.
Every bus leaves the station at time 0 and drives its routes in the given order, again and again. It returns to the station the instant it finishes a route and leaves immediately for the next one. After the last route it starts over from the first. Counting from the moment you arrive, find the earliest time at which some bus is at the station and report how long you wait.
The input is not empty and holds at most 100 data sets. No blank line separates the data sets.
A single data set has four parts.
START N, where N is the number of buses running. 1≤N≤20.END.A single line reading ENDOFINPUT follows the last data set.
Print one line per data set: the number of time units you wait after arriving until the next bus reaches the station. You hate waiting, so you board the first bus that comes back.