import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) n, m = map(int, input().split()) S = input().strip() T = input().strip() INF = float('inf') # dp[i+1][j+1]: S[i]/T[j]までの最少コスト(S, Tは0オリジン) dp = [[INF] * (len(T) + 1) for _ in range((len(S) + 1))] # 初期条件 dp[0][0] = 0 # 計算量が許すならば、自明な値はfor文を別にすると考えやすい for i in range(len(S)): dp[i+1][0] = dp[i][0] + 1 for j in range(len(T)): dp[0][j+1] = dp[0][j] + 1 # 漸化式 for i in range(len(S)): for j in range(len(T)): if S[i] == T[j]: dp[i+1][j+1] = min(dp[i][j], dp[i+1][j] + 1, dp[i][j+1] + 1) else: dp[i+1][j+1] = min(dp[i][j] + 1, dp[i+1][j] + 1, dp[i][j+1] + 1) # 求める値 print(dp[len(S)][len(T)])