#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,m,i,j,k,index,ans=0;
	cin>>n>>m;
	string s,t;
	vector<ll> v[26];
	ll first[m],last[m];
	cin>>s>>t;
	for (i=0;i<n;i++)
	{
		v[s[i]-'a'].push_back(i);
	}
	k=-1;
	for (i=0;i<m;i++)
	{
		index=upper_bound(v[t[i]-'a'].begin(),v[t[i]-'a'].end(),k)-v[t[i]-'a'].begin();
		first[i]=v[t[i]-'a'][index];
		k=first[i];
	}
	k=n;
	for (i=m-1;i>=0;i--)
	{
		index=lower_bound(v[t[i]-'a'].begin(),v[t[i]-'a'].end(),k)-v[t[i]-'a'].begin();
		index--;
		last[i]=v[t[i]-'a'][index];
		k=last[i];
	}
	/*for (i=0;i<m;i++)
		cout<<last[i]<<" ";
	cout<<endl;*/
	for (i=0;i<m-1;i++)
	{
		ans=max(ans,last[i+1]-first[i]);
	}
	cout<<ans;
	return 0;
}