#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5+5;
int mn[mxn], mx[mxn], pssbl[mxn], ans = 0;
int main()
{
    for(int i = 0; i < mxn; i++)mn[i] = 1000000;
    int n, m;
    cin>>n>>m;
    string s, t;
    cin>>s>>t;
    for(int i = m-1, j = n-1; i >= 0; j--)
        if(t[i] == s[j]){mx[i] = j;i--;}

    for(int i = 0, j = 0; i < n; i++)
        if(s[i] == t[j]){mn[j] = i; j++;}


    for(int i = 0; i < m-1; i++)
        ans = max(ans, mx[i+1]-mn[i]);

    cout<<ans<<endl;
}
