ACM Computer Factory

No attempts yetTime limit1sMemory limit128 MB

Problem

All computers used in a programming contest must be identical so that every participant competes on equal terms, so they are all built at a single factory.

Every computer consists of PP parts. A computer is complete once all PP parts are present, and only then can it be shipped.

Assembly is fully automated by NN machines. A machine takes a partially assembled computer, removes some parts, and adds others (removal is sometimes necessary because parts cannot always be added in an arbitrary order). Each machine has a performance QQ (how many computers it can process per hour), an input specification, and an output specification.

The input specification is a list of PP values, each 00, 11, or 22: for part jj, 00 means the part must be absent, 11 means it must be present, and 22 means its presence does not matter. A machine can process a partially assembled computer only if every part matches the machine's input specification.

The output specification is a list of PP values, each 00 or 11: after the machine finishes, part jj is absent when the value is 00 and present when it is 11, regardless of its previous state.

Machines are linked by production lines whose transfer time is negligible compared with processing time. A blank computer (all PP parts absent) is fed in, is processed by a sequence of machines, and leaves complete (all PP parts present).

You may route the partially assembled computers between machines in any way, provided that no machine processes more than QQ computers per hour. Under the best possible routing, how many complete computers can the factory produce per hour?

Input

The first line contains two integers PP and NN.

Each of the next NN lines describes one machine using 2P+12P + 1 integers: QiQ_i, followed by the input specification Si,1,Si,2,,Si,PS_{i,1}, S_{i,2}, \dots, S_{i,P}, followed by the output specification Di,1,Di,2,,Di,PD_{i,1}, D_{i,2}, \dots, D_{i,P}. Here QiQ_i is the performance of machine ii, Si,jS_{i,j} is its input specification for part jj, and Di,jD_{i,j} is its output specification for part jj.

Output

Print a single integer: the maximum number of complete computers the factory can produce per hour under the best routing of the production lines. If no complete computer can be produced, print 00.

Constraints

  • 1P101 \le P \le 10, 1N501 \le N \le 50, 1Qi100001 \le Q_i \le 10000