def ascan; gets.split.map(&:to_i); end N,M,P,Q = ascan n = N i = 1 loop do n -= M n -= M if P-1 <= (i-1)%12 && (i-1)%12 <= P+Q-2 if n == 0 p i exit end i += 1 end