#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% N, M = map(int, readline().split()) S = '~' + readline().rstrip().decode() T = '~' + readline().rstrip().decode() # %% dp = [[0] * (M + 1) for _ in range(N + 1)] for i in range(N + 1): dp[i][0] = i for j in range(M + 1): dp[0][j] = j for i in range(1, N + 1): for j in range(1, M + 1): dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1 - (S[i] == T[j])) print(dp[-1][-1])