P1822 Magic Fingerprint

Description

For any positive integer $n$ with at least two digits, define $\mathrm{magic}(n)$ as follows: write down the digits of $n$ in decimal order, and for each adjacent pair of digits, write the absolute value of their difference. This yields a new number; remove any leading zeros, and define the result as $\mathrm{magic}(n)$. In particular, if $n$ is a single-digit number, then $\mathrm{magic}(n)=n$. For example: $\mathrm{magic}(5913)=482$, $\mathrm{magic}(1198)=081=81$, $\mathrm{magic}(666)=00=0$. For any number $n$, repeatedly apply $\mathrm{magic}$ until $n$ becomes a single-digit number; this produces a sequence $[n,\mathrm{magic}(n),\mathrm{magic}(\mathrm{magic}(n)),\cdots]$. The final value is called the $\mathrm{magic}$ fingerprint of $n$. For example, for $n=5913$, we get the sequence $[5913,482,46,2]$. Thus, the $\mathrm{magic}$ fingerprint of $5913$ is $2$. If a number’s $\mathrm{magic}$ fingerprint is $7$, we consider it a lucky number. Now, given $A, B$, compute how many numbers in $[A,B]$ are lucky numbers.

Input Format

The input consists of two lines, one number per line. The first line is $A$, and the second line is $B$.

Output Format

Output how many numbers in $[A,B]$ are lucky numbers.

Explanation/Hint

### Constraints and Conventions - For $30\%$ of the testdata, $B \le 10^4$. - For $100\%$ of the testdata, $0 < A \le B \le 10^9$. Translated by ChatGPT 5