#t446. 产生数

产生数

Number Transformations

Problem Description

You are given an integer nn and kk transformation rules.

Rules:

  • a one-digit number may be transformed into another one-digit number;
  • the right-hand side of a rule may not be zero.

Example: n=234n=234, k=2k=2, with two rules:

  • 2⟶52 \longrightarrow 5
  • 3⟶63 \longrightarrow 6

The integer 234234 may be transformed into the following integers (including itself):

  • 234234
  • 534534
  • 264264
  • 564564

That is 44 different numbers in total.

Given nn and kk rules, find how many different integers can be produced by applying the rules any number of times (zero or more). You only need to output the count.

Input Format

The first line contains two integers n,kn, k, as described above.

The next kk lines each contain two integers xi,yix_i, y_i, describing one rule.

Output Format

One line: the number of different integers that can be produced.

Sample Input

234 2
2 5
3 6

Sample Output

4

Hint

For 100% of the data, n<1030n < 10^{30} and k≤15k \le 15.

Source: NOIP 2002, Problem 3 of the Junior (Popular) group.