n,m=map(int,input().split()) S=input() T=input() DP=[[1<<30]*m for i in range(n)] for i in range(m): if S[0]==T[i]: DP[0][i]=i else: DP[0][i]=i+1 for i in range(n): if S[i]==T[0]: DP[i][0]=i else: DP[i][0]=i+1 for i in range(n): for j in range(m): if S[i]==T[j]: if i-1>=0 and j-1>=0: DP[i][j]=min(DP[i-1][j-1],DP[i][j]) if i-1>=0: DP[i][j]=min(DP[i-1][j]+1,DP[i][j]) if j-1>=0: DP[i][j]=min(DP[i][j-1]+1,DP[i][j]) else: if i-1>=0 and j-1>=0: DP[i][j]=min(DP[i-1][j-1]+1,DP[i][j]) if i-1>=0: DP[i][j]=min(DP[i-1][j]+1,DP[i][j]) if j-1>=0: DP[i][j]=min(DP[i][j-1]+1,DP[i][j]) print(DP[n-1][m-1])