N,M = list(map(int, input().split(' '))) S = input() T = input() dp = [] for _ in range(N+1): dp.append([0 for _ in range(M+1)]) for i in range(N): dp[i][0] = i for j in range(M): dp[0][j] = j for i in range(N): for j in range(M): cost = 0 if S[i] == T[j] else 1 dp[i+1][j+1] = min(dp[i+1][j] + 1, dp[i][j+1] + 1, dp[i][j] + cost) print(dp[N][M])