// Author: HarshKumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e6 + 6, M = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    ll n, m, ans = 0;
    string s, t;
    cin >> n >> m >> s >> t;
    vector<ll> a;
    for (ll i = 0, j = 0; j < m; i++)
        if (s[i] == t[j])
            a.push_back(i), j++;
    for (ll i = n - 1, j = m - 1; j; i--)
        if (s[i] == t[j])
            ans = max(ans, i - a[j - 1]), j--;
    cout << ans;
}