dude.inJoe likes highway driving on the edge…of an empty gas tank, that is. Joe's fuel gauge only registers the number of eighths of a tank remaining (i.e., 1/8, 2/8, …8/8). One of his favorite stretches of highway has exits spaced evenly so that he can reach the same number of exits, k, for each eighth of a tank of gas (for example, with 2/8 tank of gas, he can reach 2k exits). Exits may or may not have a gas station. Given the number of eighths of a tank of gas in Joe’s car, the value of k, and the locations of the gas stations, determine the farthest exit with a gas station that Joe can reach using only the gas in his tank (i.e., no fill-ups of the tank are allowed en route). Of course, it is possible that Joe will run out of gas…
Each data set begins with a line containing a single integer t, indicating the number of eighths of a tank of gas in Joe’s car (where 1 <= t <= 8). The next line contains a single integer k, indicating how many highway exits can be reached per 1/8 tank of gas, (where 1 <= k <= 8). Starting on the next line of the input, and possibly continuing over several input lines, will appear the numbers of the exits at which gas stations are located. Joe’s first available exit is always 1, and the exits are numbered consecutively. The furthest possible exit number is 1000. The list of exits will be terminated by a –1. Exit numbers in a particular data set will not be duplicated but may be listed in any order. Your program should stop processing data sets when it reaches a value of 0 for t.
For each data set, output the exit number of the last gas station that the car can reach. If the car runs out of gas before it can reach a gas station, output that information instead. Follow the format illustrated in the sample output.
8 1 1 2 7 4 5 6 -1 1 8 44 45 13 43 50 -1 0
Stop for gas at exit 7. Dude! Ran out of gas.