Under construction

Still being built. Things may change

Idea Search

Time limit5sMemory limit1024 MB

Summary
Given blocks that append literal strings or copy earlier substrings of the growing text, count how many times pattern p occurs in the final text.
Level

Hard8 of 10

Topics
String matching, String, Implementation
Solved
No attempts yet

Problem

In the year 3019, archaeologists dug up an ancient city called Innopolis and found a hard disk. It holds a file that probably contains the texts of all problems from the All-Russian informatics olympiads.

After studying the file, they found that its contents encode a string tt of lowercase English letters. The problem texts are quite long and contain many repetitions, so the file was stored on disk in compressed form. The following algorithm unpacks it.

During unpacking, a string tt of lowercase English letters is built. It starts empty. The compressed file consists of nn blocks, which must be processed in order. Each block has one of two types.

  • Block 1 $w$, where ww is a string. Processing this block appends ww to the end of tt.
  • Block 2 $pos$ $len$, where pospos and lenlen are positive integers. Number the characters of tt starting from 1. Processing this block appends, one by one, lenlen consecutive characters of tt starting at position pospos to the end of tt. If lenlen is large enough, some of the characters just appended may be used again while processing the same block.

The researchers want to know how many times an idea appeared in the olympiads. They form a string pp of lowercase English letters and count how many times pp occurs as a substring of the unpacked string tt.

A string pp of length mm occurs in tt starting at position ii if the mm consecutive characters of tt starting at the ii-th character form pp. For example, aba occurs in ababaaba three times, at positions 1, 3, and 6.

Write a program that counts the occurrences of pp in the unpacked string tt.

Input

The first line contains natural numbers mm and nn, the length of string pp and the number of blocks in the compressed text (1≤m≤2⋅1051 \le m \le 2 \cdot 10^5, 1≤n≤1041 \le n \le 10^4).

The second line contains the nonempty string pp, which consists of lowercase English letters.

Each of the next nn lines describes one block in the format above. For blocks of the first type, the appended string ww is nonempty, and the total length of all strings ww in blocks of the first type does not exceed 2⋅1052 \cdot 10^5. For blocks of the second type, when the block is processed, tt already has at least pospos characters. The length of the unpacked text does not exceed 101510^{15} characters.

Output

Print one integer, the number of occurrences of the string pp in the text.

Hint

Unpacking the example file produces these strings in turn: empty string →\to ab →\to ababa →\to ababaaba →\to ababaabaababaaba.

The string aba occurs in ababaabaababaaba 6 times.

Examples1

  1. Example 1

    Input
    3 4
    aba
    1 ab
    2 1 3
    2 3 3
    2 1 8
    
    Expected output
    6