using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; class Template { static Scanner sc; public static void Main(string[] args) { sc = new Scanner(); var nm = sc.nextIntArray(); int n = nm[0], m = nm[1]; string S = sc.next(); string T = sc.next(); int[,] dp = new int[n + 1, m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i, j] = (int)1e9; } } //dp[i, j] : Sのi文字目までと,Tのj文字目までのレーベンシュタイン距離 for (int i = 0; i <= n; i++) { dp[i, 0] = i; } for (int i = 0; i <= m; i++) { dp[0, i] = i; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (S[i - 1] != T[j - 1]) { dp[i, j] = Math.Min(dp[i, j], dp[i - 1, j - 1] + 1); } else { dp[i, j] = Math.Min(dp[i, j], dp[i - 1, j - 1]); } dp[i, j] = Math.Min(dp[i, j], dp[i - 1, j] + 1); dp[i, j] = Math.Min(dp[i, j], dp[i, j - 1] + 1); } } Console.WriteLine(dp[n, m]); } } public class Scanner { public Scanner() { } public string next() { return Console.ReadLine(); } public int nextInt() { return int.Parse(next()); } public double nextDouble() { return double.Parse(next()); } public long nextLong() { return long.Parse(next()); } public string[] nextArray() { return next().Split(' '); } public int[] nextIntArray() { return Array.ConvertAll(nextArray(), e => int.Parse(e)); } public long[] nextLongArray() { return Array.ConvertAll(nextArray(), e => long.Parse(e)); } public double[] nextDoubleArray() { return Array.ConvertAll(nextArray(), e => double.Parse(e)); } }