import sys sys.setrecursionlimit(10**6) slen, tlen = map(int, input().split()) s = input() t = input() # dp = [[None] * tlen for _ in range(slen)] # def dfs(a=slen - 1, b=tlen - 1): # if a == b == -1: # return 0 # if a == -1: # return b + 1 # if b == -1: # return a + 1 # # if dp[a][b] is not None: # return dp[a][b] # r = min( # dfs(a - 1, b) + 1, # dfs(a, b - 1) + 1, # dfs(a - 1, b - 1) + (s[a] != t[b]), # ) # dp[a][b] = r # return r # print(dfs()) dp = [[0] * (tlen + 1) for _ in range(slen + 1)] for b in range(tlen): dp[-1][b] = b + 1 for a in range(slen): dp[a][-1] = a + 1 for a in range(slen): for b in range(tlen): dp[a][b] = min( dp[a - 1][b] + 1, dp[a][b - 1] + 1, dp[a - 1][b - 1] + (s[a] != t[b]), ) print(dp[a][b])