This page is still under construction.

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

Key Maker

Interview

Time limit2sMemory limit512 MB

Summary
For each test case, count how many trash keys have the same cut count and can be deepened, or already match, the customer key.
Level

Easy2 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

Hassan makes copies of keys for a living. A customer brings in a safe box key and asks for a few copies of it. A key has several cuts of different depths. The picture above is a safe box key with 3 cuts. To make a copy, Hassan has to put the same number of cuts on a blank key, with exactly the same sequence of depths.

When Hassan started out, he wasted many blank keys while making copies. Most of the keys he finished did not fit the customer key, so he could not sell them. He collected those keys in a trash box, and now he wants to reuse them.

When a new customer arrives, Hassan digs through the trash box, takes out every key that has the same number of cuts as the customer key, and counts how many of them can be fitted to the customer key. A key can be fitted if its sequence of cut depths already equals the customer key, or if cutting some of its cuts deeper produces that sequence. A cut cannot be made shallower once it is made. In any two keys with the same number of cuts, assume the cuts sit at the same positions.

Input

The input holds several test cases. The first line of each test case has the number of cuts in the customer key, mm (1≤m≤101 \le m \le 10), and the number of trash box keys with that many cuts, nn (1≤n≤1001 \le n \le 100), separated by a space. The second line has the mm cut depths of the customer key, separated by spaces. Each of the next nn lines has the mm cut depths of one trash box key. The cut depths of all n+1n + 1 keys are one-digit positive integers, given from left to right. The last line of the input is 0 0 and is not processed.

Output

For each test case, print on one line the number of trash box keys that already equal the customer key or can be cut deeper to match it.

Examples2

  1. Example 1

    Input
    4 1
    3 2 1 3
    2 2 1 2
    4 1
    4 2 2 2
    3 2 2 3
    5 3
    2 2 4 2 2
    2 3 4 3 2
    1 1 3 2 2
    2 2 2 2 2
    0 0
    
    Expected output
    1
    0
    2
    
  2. Example 2

    Input
    1 1
    5
    5
    0 0
    
    Expected output
    1