CSES - Aalto Competitive Programming 2024 - wk3 - Wed - Spin-Pop-Squeeze-Clap
  • Time limit: 6.00 s
  • Memory limit: 512 MB

In the popular Spin-Pop-Squeeze-Clap party game, you have got blocks with spinning counters that can count from 0 to 9 and then back to 0.

Each block has a handle with which you can turn the counter so that it shows whatever number you want. And there is a microphone that recognizes hand claps: every time you clap hands, all blocks will increment their counters by one.

So this is how the game goes. Your opponent takes some number of blocks and adjusts their counters so that they represent some sequence of digits A. And the opponent also tells you what is the sequence of digits B that you have to reach. So the opponent might give you e.g. a sequence of counters that says 111222 and asks you to turn it into a sequence of counters that says 283303.

You can make the following moves, in any order:

  • Spin: take any one of the blocks and turn it so that it shows whatever number you want. So you could turn 111222 e.g. into 171222.
  • Pop: remove any block from the sequence. So you could turn e.g. 171222 into 17222.
  • Squeeze: add a new block in the middle of the sequence or at either end, showing whatever number you want. So you could turn e.g. 17222 into 172292.
  • Clap: clap your hands so that all counters increment by one (modulo 10). So you could turn e.g. 172292 into 283303.

Your task is to find out how to get from A to B with the minimum number of these moves.

Input

The input is two lines, the first line contains A and the second line contains B. Both of these are sequences of digits 0–9.

Output

Output one line with number N, the smallest possible number of moves (spin, pop, squeeze, clap) needed to turn A into B.

Limits

The lengths of A and B are at least 1 and at most 1000 digits.

Example 1

Input:

111222
283303

Output:

4

Explanation:

  • 111222 → 171222 (spin)
  • 171222 → 17222 (pop)
  • 17222 → 172292 (squeeze)
  • 172292 → 283303 (clap)

Example 2

Input:

123123
54545

Output:

4

Explanation:

  • 123123 → 12323 (pop)
  • 12323 → 23434 (clap)
  • 23434 → 34545 (clap)
  • 34545 → 54545 (spin)