#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define nl cout<<endl

#define pii pair<ll,ll>
#define vi  vector<ll>
#define vii vector<pii>
#define mi  map<ll,ll>
#define all(a)  (a).begin(),(a).end()

#define pb push_back
#define ff first
#define ss second
#define hell 1000000007

#define test4(x,y,z,a) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<"		a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<"		y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 300009

ll power(ll a,ll b,ll m)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}
int main()
{	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
        
	ll n,m;cin>>n>>m;
	string a,b;cin>>a>>b;

	ll fi = b[0],last = b[m-1];
	rep(i,0,n) {
		if( a[i] == fi) {
			fi = i;
			break;
		}
	}
	for(ll i=n-1;i>=0;i--) {
		if( a[i] == last) {
			last = i;
			break;
		}
	}

	if( m == 2) {
		cout << last - fi << endl;
	}
	else {
		ll mini[m+2],maxi[m+2];
		mini[0] = fi;

		ll curr = 1;
		rep(i,fi+1,n) {
			//test3("mc",i,curr);
			if( a[i] == b[curr]) {
				mini[curr] = i;
				curr ++;
				//i ++;
			}
			else {
				//i ++;
			}
			if(curr == m)
				break;
		}

		maxi[m-1] = last;
		curr = m-2;
		for(ll i=last-1;i>=0;i--) {
			if( a[i] == b[curr]) {
				maxi[curr] = i;
				curr --;
				//i --;
			}
			else;
				//i--;
			if(curr < 0)
				break;
		}

	/*	rep(i,0,m)
			test3(i,mini[i],maxi[i]);*/
		ll ans = 0;
		rep(i,0,m-1) {
			if( mini[i] < mini[i+1])
				ans = max(ans,mini[i+1] - mini[i]);
			if( mini[i] < maxi[i+1])
				ans = max(ans,maxi[i+1] - mini[i]);
			if( maxi[i] < mini[i+1])
				ans = max(ans,mini[i+1] - maxi[i]);
			if( maxi[i] < maxi[i+1])
				ans = max(ans,maxi[i+1] - maxi[i]);
		}
		cout << ans << endl;
	}
}
