Zoned Out

Time limit1sMemory limit128 MB

Problem

"You want to build an office building in our town? Wonderful! We're so glad to have you!" The mayor beamed as he led me back into City Hall. "We just need to make sure your building permit application meets all of our town's zoning regulations. We can check that right away."

He led me through a door labelled "Zoning". I was staggered by a huge room filled with row after row of desks. At each desk sat a clerk with a logbook, an inbox, and a generous supply of pencils and erasers.

The mayor took my application form, then produced a sheet of paper with several lines of numbered boxes. He dropped it into the inbox of the nearest desk and said, "We run this past our zoning clerks, and if it comes back to this desk with every box checked, your application is approved." The clerk grabbed the form, checked a few boxes, wrote a few lines in his logbook, then made copies at a nearby copier and handed them to several other desks. Those clerks each added some check marks and erased others, wrote in their logbooks, made more copies, and passed them on. Soon copies were flying all over the room.

"But none of them actually looked at my application!" I said.

"Oh no," said the mayor. "Each clerk has carefully studied a few parts of our zoning regulations. Some are convinced that any application automatically satisfies certain rules; they also believe that no application can ever satisfy certain others. So each clerk simply checks off some boxes and erases the check marks from some others."

"We're very proud," he added, "of how decisive our zoning process is. We recruit from the nearby law school and hire only the most officious, argumentative junior students."

"How do they decide where the marked-up copies go?" I asked.

"I'm not sure," said the mayor. "I think they mostly hand them to the fellow clerks they dislike the most, trying to pile more work on their rivals." Just then a sheet landed on the nearest desk. "Let's see how you're doing," he said, picking it up. "No, still four boxes empty. We'll have to wait and see."

"It seems this could go on forever," I sighed.

"No," said the mayor. "Each clerk keeps a logbook of every version of your application they have ever passed on. When a form arrives, they first add every check mark that appeared on any earlier version they sent, then apply their own marks and erasures. If the result matches a version they already sent, they will not send it a second time."

So the process is finite. Stated precisely:

  • The form has a fixed number of boxes, numbered from $0$. A version of the form is the set of boxes currently checked.
  • Clerks are numbered from $0$. Each clerk has a fixed set of boxes it always checks, a fixed set of boxes it always erases, and a list of clerks to whom it forwards copies.
  • The application begins as a blank form (no boxes checked) placed in clerk $#0$'s inbox.
  • When a clerk receives a form, it builds a new version by (1) taking the incoming form together with every version it has previously sent, (2) checking all of its always-checked boxes, and (3) erasing all of its always-erased boxes. If this new version is identical to one the clerk has already sent, the clerk discards it; otherwise the clerk records it and forwards a copy to each clerk on its list.

Report the boxes checked on the last version that leaves clerk $#0$'s desk.

Input

The input contains one or more problem sets.

The first line of each set contains two positive integers: the number of boxes on the form and the number of clerks in the office. A line containing two zeros marks the end of the input and is not a problem set.

Each set then gives three lines for every clerk, starting with clerk $#0$ (boxes and clerks are both numbered from $0$). Each line holds zero or more integers separated by whitespace:

  • the boxes that this clerk always checks,
  • the boxes that this clerk always erases,
  • the clerks to whom this clerk forwards copies of the altered form.

Output

For each problem set, print one line with the numbers of the boxes checked on the last copy of the form that leaves clerk $#0$'s desk, in ascending order and separated by single spaces. If no boxes are checked, print an empty line.