#P12289. Minimum Modification Cost for Consecutive Permutation Substring

    ID: 14399 Type: Default 1000ms 256MiB

Minimum Modification Cost for Consecutive Permutation Substring

Minimum Modification Cost for Consecutive Permutation Substring

Given an n-digit decimal number m (without leading zeros), you can change any digit ai to any other digit bi at a cost of |bi - ai|. Determine the minimum total cost to modify m so that there exists a contiguous substring of length 10, which is a permutation of digits 0 through 9 (each digit appears exactly once). Note that the modified number must not have a leading zero.

The substring can be located anywhere in the number. However, if the substring starts at the first digit, then the digit at that position cannot be changed to 0 (because the overall number must not have a leading zero).

If it is impossible to obtain such a substring (for example, if n < 10), output -1.

inputFormat

The input consists of a single line containing the decimal number m (as a string without spaces). The number does not have any leading zeros.

outputFormat

Output a single integer: the minimum total cost required to modify m such that there is a contiguous substring of length 10 containing each digit from 0 to 9 exactly once. If it is impossible, output -1.

sample

1234567890
0