| Digital Roulette | 
 0 that represents the maximum number in the roulette.
 0 that represents the maximum number in the roulette.
Of course, the roulette behaves digitally. As a matter of fact, John designed its way to choose a value in the 
interval 0..N (the result of spinning the roulette) with a digital trigger that moves the roulette with a force that depends 
on an integer value x randomly chosen in the interval 0..M, where M 0 (M is the maximal appliable force). 
The roulette turns around a distance equivalent to P(x), where P is a polynomial with integer coefficients. One distance 
unit represents a displacement of one roulette number, counting clockwise.
 0 (M is the maximal appliable force). 
The roulette turns around a distance equivalent to P(x), where P is a polynomial with integer coefficients. One distance 
unit represents a displacement of one roulette number, counting clockwise. 
It is clear that some result values may be produced by different chosen force values. Also, depending on the mechanism 
parameters, some numbers in the roulette may be not attainable regardless of the force value. For example, if N = 7, M = 5 
and 
P(x) = x2 + 1, the mechanism can generate only three different results:
 
John wants to know how many different result values may be attained by his mechanism. Can you help him?
There are several cases to analyze. Each case is described by three lines:
 N
 N  107, 
0
 107, 
0  M
 M  105).
 105).
 k
 k  10).
 10).
 ai
 ai  N for 
each 
0
 N for 
each 
0  i
 i  k. If k > 0 then you may assume that 
ak
 k. If k > 0 then you may assume that 
ak  0.
 0.
The last test case is followed by a line containing two zeros.
For each case, print one line indicating how many different numbers are attainable by John's mechanism.
7 5 2 1 0 1 99 10 0 5 99 10 1 5 25 99 10 1 3 29 99 10 2 3 29 31 0 0
3 1 4 11 10