Arranging Hat

Each of n m-digit strings may have individual digits rewritten; find the minimum number of digit changes so the sequence becomes nondecreasing.

Hard8Dynamic programmingGreedyStringImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

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 mm 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.

Input

The first line contains the number of numbers nn and the number of digits in each number mm (1n401 \le n \le 40, 1m4001 \le m \le 400).

Each of the next nn lines contains one integer vv (0v<10m0 \le v < 10^m). Every number is padded with leading zeros to exactly mm digits.

Output

Print the smallest number of digits that have to be changed to make the list sorted.