This page is still under construction.

Parts of this page are still being built. What you see may change.

It's not a Bug, it's a Feature!

Interview

Time limit1sMemory limit128 MB

Summary
Each bug state is a bitmask; find the shortest total patch time from all bugs present to no bugs, where patches have presence and absence preconditions.
Level

Medium7 of 10

Topics
Graph, Shortest path, Bit manipulation, BFS
Solved
No attempts yet

Problem

It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right? Or a CD player that plays only CDs with country music on them? Probably not. But for software systems it seems to be acceptable if they do not perform as they should. In fact, many software companies have adopted the habit of sending out patches to fix bugs every few weeks after a new product is released (and even charging money for the patches).

Tinyware Inc. is one of those companies. After releasing a new word processing software this summer, they have been producing patches ever since. Only this weekend did they realize a big problem with the patches they released. While every patch fixes some bugs, it often relies on other bugs being present in order to be installed. This happens because, to fix one bug, a patch exploits the special behavior of the program caused by another bug.

More formally, the situation looks like this. There are nn bugs B={b1,b2,…,bn}B = \{b_1, b_2, \ldots, b_n\} in the software, and mm patches p1,p2,…,pmp_1, p_2, \ldots, p_m. To apply patch pip_i, all bugs in Bi+⊆BB_i^+ \subseteq B must be present and all bugs in Bi−⊆BB_i^- \subseteq B must be absent (of course Bi+∩Bi−=∅B_i^+ \cap B_i^- = \varnothing). Applying the patch then removes the bugs in Fi−⊆BF_i^- \subseteq B (those that were present) and introduces the new bugs in Fi+⊆BF_i^+ \subseteq B (again with Fi−∩Fi+=∅F_i^- \cap F_i^+ = \varnothing).

Starting from the original version, which contains every bug in BB, is it possible to apply a sequence of patches that results in a bug-free version of the software? If so, and assuming each patch takes a given time to apply, how long does the fastest sequence take?

Input

The input contains several product descriptions. Each description starts with a line containing two integers nn and mm, the number of bugs and the number of patches, respectively, with 1≤n≤201 \le n \le 20 and 1≤m≤1001 \le m \le 100. This is followed by mm lines describing the patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, followed by two strings of nn characters each.

The first string describes which bugs must be present or absent before the patch can be applied. Its ii-th character is + if bug bib_i must be present, - if bug bib_i must be absent, and 0 if it does not matter whether the bug is present.

The second string describes which bugs are fixed and introduced by the patch. Its ii-th character is + if bug bib_i is introduced by the patch, - if bug bib_i is removed by the patch (when it was present), and 0 if bug bib_i is not affected (if it was present before, it still is; if it wasn't, it still isn't).

The input is terminated by a description whose first line is 0 0; this description must not be processed.

Output

For each product description, first print a line Product X, where X is the product's number (starting from 1). Then, if there is a sequence of patches (a patch may be used several times) that removes all nn bugs from a product that starts with all nn bugs present, print Fastest sequence takes S seconds., where SS is the minimum total time. Otherwise, print Bugs cannot be fixed..

Separate the output of consecutive products with a blank line.

Examples3

  1. Example 1

    Input
    3 3
    1 000 00-
    1 00- 0-+
    2 0-- -++
    4 1
    7 0-0+ ----
    0 0
    
    Expected output
    Product 1
    Fastest sequence takes 8 seconds.
    
    Product 2
    Bugs cannot be fixed.
    
  2. Example 2

    Input
    1 1
    5 0 -
    0 0
    
    Expected output
    Product 1
    Fastest sequence takes 5 seconds.
    
  3. Example 3

    Input
    1 1
    3 - -
    0 0
    
    Expected output
    Product 1
    Bugs cannot be fixed.