/* template.cpp {{{ */ #include using namespace std; // #define int long long #define GET_MACRO(a, b, c, d, NAME, ...) NAME #define REP1(n) REP2(i_, n) #define REP2(i, n) REP3(i, 0, n) #define REP3(i, a, b) REP4(i, a, b, 1) #define REP4(i, a, b, s) for (long long i = (a); i < (long long)(b); i += (long long)(s)) #define RREP1(n) RREP2(i_, n) #define RREP2(i, n) RREP3(i, 0, n) #define RREP3(i, a, b) RREP4(i, a, b, 1) #define RREP4(i, a, b, s) for (long long i = (b) - 1; i >= (long long)(a); i -= (long long)(s)) #define rep(...) GET_MACRO(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define rrep(...) GET_MACRO(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__) #define fs first #define sc second #define all(c) std::begin(c), std::end(c) #define dump(...) std::cerr << "("#__VA_ARGS__") = " << std::make_tuple(__VA_ARGS__) << "\n" #define y0 y0_ #define y1 y1_ #define yn yn_ using uint = unsigned; using ll = long long; using ull = unsigned long long; using ld = long double; template using V = vector; template using V2 = vector>; template using V3 = vector>; template using V4 = vector>; using vi = V; using vu = V; using vl = V; using vul = V; using vd = V; using vld = V; template using minheap = priority_queue, greater>; template using maxheap = priority_queue; template inline constexpr T sq(T x){ return x * x; } template inline T& chmin(T &x, U y){ if (x > y) x = y; return x; } template inline T& chmax(T &x, U y){ if (x < y) x = y; return x; } const string delimiter = " "; void setDelimiter(const string &d){ const_cast(delimiter) = d; } template typename enable_if<(N >= sizeof...(T))>::type print_tuple(ostream &, const tuple &, const string &){ } template typename enable_if<(N < sizeof...(T))>::type print_tuple(ostream &os, const tuple &x, const string &delim = delimiter){ os << (N > 0 ? delim : "") << get(x); print_tuple(os, x, delim); } template ostream &operator<<(ostream &os, const tuple &x){ if (&os == &cerr) print_tuple<0, T...>(os, x, " "); else print_tuple<0, T...>(os, x); return os; } template ostream &operator<<(ostream &os, const pair &x){ return os << x.first << delimiter << x.second; } /* }}} */ int n, m; string s, t; int dp[1010][1010]; signed main() { cin >> n >> m >> s >> t; fill_n(*dp, 1010 * 1010, 1 << 28); dp[0][0] = 0; rep(i, n + 1) rep(j, m + 1){ if (s[i] == t[j]) chmin(dp[i + 1][j + 1], dp[i][j]); chmin(dp[i + 1][j + 1], dp[i][j] + 1); chmin(dp[i + 1][j], dp[i][j] + 1); chmin(dp[i][j + 1], dp[i][j] + 1); } cout << dp[n][m] << endl; }