You are given an *n* × *m* rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table

 

we obtain the table:

 

A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
The first line contains two integers  — *n* and *m* (1 ≤ *n*, *m* ≤ 100).

Next *n* lines contain *m* small English letters each — the characters of the table.
import sys
import math

MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65


def getInt():
    return int(sys.stdin.readline().rstrip())


def getInts():
    return map(int, sys.stdin.readline().rstrip().split(" "))


def getString():
    return sys.stdin.readline().rstrip()


def printOutput(ans):
    sys.stdout.write()
    pass


def orderedVal(grid, curValues, column):
    if column == 0:
        return 0
    for i in range(len(grid)):
        if grid[i][curValues] > grid[i][column]:
            return math.inf
    return 0


def isLex(grid, column):
    for i in range(1, len(grid)):
        if grid[i][column] < grid[i - 1][column]:
            return False
    return True


def getLexPairs(grid, column):
    lPairs = set()
    for i in range(1, len(grid)):
        if grid[i][column] == grid[i - 1][column]:
            lPairs.add((i - 1, i))
    return lPairs


def compare(grid, column, lPairs):
    nextlPairs = set()
    """either returns remaining lPairs, True, or False"""
    for a, b in lPairs:
        # print("hi", a, b)
        # print(grid[a][column], grid[b][column])
        if grid[a][column] > grid[b][column]:
            return False
        elif grid[a][column] == grid[b][column]:
            nextlPairs.add((a, b))
    return nextlPairs


def solve(grid):
    ans = len(grid[0])
    for column in range(len(grid[0])):
        if ans <= column:
            break
        if isLex(grid, column):
            removeCol = 0
            lPairs = getLexPairs(grid, column)
            # print(column, lPairs)
            for nextColumn in range(column + 1, len(grid[0])):
                k = compare(grid, nextColumn, lPairs)
                # print('comp',k)
                if k == False:
                    removeCol += 1
                elif len(k) == 0:
                    break
                else:
                    lPairs = k
            ans = min(ans, column + removeCol)

    return ans


def readinput():
    n, m = getInts()
    grid = []
    for _ in range(n):
        grid.append(getString())

    print(solve(grid))


readinput()
