t=gets.to_i n=gets.to_i a=(0...n).map{gets.to_i} d=[0] f=->(x,b){y=t-x%t;b>y ?x+b+y:x+b} (1...1<0?f[d[h^1<