The Arranging Hat has a comfortable position. The work has high impact, the hat decides everything on its own, and it gets 364 days of holiday every year. The hat still wants more, so it is going after a tenured professorship.
In its spare time the hat reads computer science papers, and being an arranging hat, its interest is sorting algorithms. What it reads now is lossy sorting. Such an algorithm usually drops part of the input so that the values that remain come out sorted. Dropsort works that way.
The hat plans to go one step further. Its algorithm throws away no number and moves no number: every value stays on the line it started on. Instead the hat rewrites single digits until the list is sorted, meaning the values never decrease from the first line to the last. One change replaces one digit of one number by a different digit, and after the changes every number still has exactly m digits. A leading 0 counts as a digit.
The loss of this sort is the number of digit positions the hat rewrites. Compute the smallest number of digits that have to be changed to make the list sorted.