Date Bugs

Time limit1sMemory limit128 MB

Summary
Given up to 20 wrap-around year displays, find the smallest real year below 10000 consistent with every computer's shown value.
Level

Medium4 of 10

Topics
Brute force, Math, Implementation, Number theory
Solved
No attempts yet

Problem

Many computers store the year using only a few digits or a fixed-size counter, so after a certain point the displayed year wraps around. A computer that shows only two digits, for example, jumps from 1999 back to 1900. Some systems instead store the number of seconds elapsed since a fixed reference time in a 32-bit integer; after about 2322^{32} seconds (roughly 136 years) the value wraps back to that reference time.

Each computer ii has a bug year bib_i. While the real year is below bib_i the computer shows it correctly, but the moment the real year reaches bib_i the display wraps to aia_i (instead of bib_i) and keeps counting up, wrapping again every time it would reach bib_i. Thus computer ii can only ever display a year in the range [ai,bi)[a_i, b_i), and when the real year is YY its display is

displayi(Y)=ai+((Y−ai) mod (bi−ai))\text{display}_i(Y) = a_i + \big((Y - a_i) \bmod (b_i - a_i)\big)

Several computers each report the year yiy_i they currently display together with their bug parameters aia_i and bib_i. Assuming every computer refers to the same real year, find the earliest real year consistent with all of them. Because a computer cannot display a year before aia_i and cannot predate its own construction, the real year is always at least the maximum of the aia_i.

Input

The input consists of several test cases. Each test case begins with a line containing an integer nn (1≤n≤201 \le n \le 20), the number of computers. Each of the next nn lines contains three integers yiy_i, aia_i, bib_i (0≤ai≤yi<bi<100000 \le a_i \le y_i < b_i < 10000): yiy_i is the year the computer displays, bib_i is the first year at which the bug occurs (the first year the computer can no longer display), and aia_i is the year it wraps to instead.

The input ends with a test case where n=0n = 0, which must not be processed.

Output

For the kk-th test case, first print Case #k:. If a real year consistent with every computer exists, print The actual year is z., where zz is the smallest such year that is at least the maximum of the aia_i. If no such year below 10000 exists, print Unknown bugs detected. instead. Print a blank line between consecutive test cases.

Examples2

  1. Example 1

    Input
    2
    1941 1900 2000
    2005 1904 2040
    2
    1998 1900 2000
    1999 1900 2000
    0
    
    Expected output
    Case #1:
    The actual year is 2141.
    
    Case #2:
    Unknown bugs detected.
    
  2. Example 2

    Input
    1
    1941 1900 2000
    0
    
    Expected output
    Case #1:
    The actual year is 1941.