Magic Barrier

Time limit1sMemory limit512 MB

Summary
For each fired shell, walk through N layers where layer i repeats a binary pattern; the shell at position P shifts by D per layer and survives only if every visited cell is 1. Count survivors.
Level

Medium6 of 10

Topics
Math, Implementation, Number theory, Simulation
Solved
No attempts yet

Problem

The Empire and the Kingdom have been on bad terms for a long time. A week ago the Empire declared war on the Kingdom, and it has been winning every battle, advancing all the way to the Kingdom's castle.

The castle is protected by a magic barrier, so attacking it requires a magic cannon that can pierce the barrier. The barrier consists of N layers stuck together, numbered 1 to N from the outside of the castle inward. Each layer is 1 unit thick and extends infinitely far to the left and right. Layer i (1 ≤ i ≤ N) has a pattern of horizontal length Li. The pattern is made of cells of length 1, each written as 1 or 0. A 1 is a cell a magic shell can pass through, and a 0 is a cell a magic shell cannot pass through. On layer i, this pattern repeats infinitely every Li cells.

For example, a layer with pattern 01101 is drawn below. A magic shell cannot pass through cells 0 and 3, and can pass through cells 1, 2, and 4. Because the pattern repeats forever, a shell also cannot pass through cells 5 and 8 or cells -5 and -2, and can pass through cells 6, 7, and 9 or cells -4, -3, and -1. If a shell reaches a cell it cannot pass through, it is destroyed on the spot.

The Empire set up magic cannons 1 unit away from layer 1 and fired M magic shells in total. Shell i is fired from position Pi in direction Di. Each second it passes through one layer in order, and each time it passes through a layer it shifts sideways by Di. For example, if a magic cannon is fired from position 3 in direction -2, the shell is at position 1 when it reaches the first layer and at position -1 when it reaches the second layer. A shell teleports each second, so it is not affected by other cells along the way. Once a magic shell passes through every layer, it damages the Kingdom's castle.

Given the patterns of the Kingdom's castle and the positions and directions of the magic shells the Empire fired, how many of those shells damaged the castle?

Input

The first line gives the number of layers of the magic barrier N (1 ≤ N ≤ 10,000) and the number of magic shells the Empire fired M (1 ≤ M ≤ 200,000), separated by a space.

Each of the next N lines describes the magic barrier. Line i+1 gives the pattern length Li (1 ≤ Li ≤ 6) of layer i and the pattern of length Li, separated by a space.

Each of the next M lines describes a magic shell the Empire fired. Line N+i+1 gives the launch position Pi and the launch direction Di of shell i (-100,000 ≤ Pi, Di ≤ 100,000), separated by a space.

Every number in the input is an integer.

Output

On the first line, output the number of magic shells that damaged the Kingdom's castle.

Examples1

  1. Example 1

    Input
    3 7
    4 0111
    3 101
    5 11101
    2 0
    2 -1
    -2 1
    -10 5
    0 0
    0 1
    9 -4
    
    Expected output
    4