cho.sh
Notes
Loading...

Dial Lock

Time limit

2s

Memory limit

128 MB

Problem

A laptop is protected with a dial lock. The lock consists of NNN circular disks. Each disk shows one digit from 0 to 9, and because each disk is circular, 0 and 9 are adjacent.

In one operation, choose a non-empty contiguous block of at most three disks. Rotate every chosen disk in the same direction by the same number of steps, where the number of steps is between 1 and 3. The direction may be clockwise or counterclockwise.

Given the current state of the lock and the password, find the minimum number of operations needed to open the lock.

For the state 555 and password 464, rotating each disk separately would take 3 operations. Instead, rotate all three disks one step counterclockwise to make 444, then rotate the second disk two steps clockwise to make 464, for a total of 2 operations.

Input

The first line contains NNN, the length of the password and the number of disks in the lock. NNN is a positive integer at most 100.

The second line contains the current state of the lock, and the third line contains the password. Both strings have length NNN and consist only of digits from 0 to 9.

Output

Print the minimum number of operations needed to open the lock.