#t446. 产生数
产生数
Number Transformations
Problem Description
You are given an integer and 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: , , with two rules:
The integer may be transformed into the following integers (including itself):
That is different numbers in total.
Given and 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 , as described above.
The next lines each contain two integers , 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, and .
Source: NOIP 2002, Problem 3 of the Junior (Popular) group.