#include<bits/stdc++.h>
#include <stdio.h>
#define endl "\n"
#define pb push_back
#define ll long long
#define dbg cout<<"**"<<endl;
#define d1(x)  cout << #x << "--> " << x << endl
#define d2(x, y)  cout << #x << "--> " << x << " | " << #y << "--> " << y <<endl
#define d3(x, y,z)  cout << #x << "--> " << x << " | " << #y << "--> " << y <<" | " << #z << "--> "<< z<< endl
#define vpll vector<pair<ll,ll>>
#define vll vector<ll>
#define ff first
#define ss second
#define mp make_pair
#define sz size()
#define pll pair<ll,ll>
#define umap map<ll,ll>
#define print_ans cout<<ans<<endl;
#define all(x) (x).begin(), (x).end()
#define mem(name, value) memset(name, value, sizeof(name))
#define mem2(v,x) fill(all(v), x);
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
const ll mod=1e9+7;
const ll maxn=1e5+1;
void swap(ll *xp, ll *yp) 
{ 
    ll temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
}
void print(vector<ll> v)
{
    ll i,j,n=v.size();
    for(i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
void scan(vll &v,ll n)
{
    for(ll i=0;i<n;i++)
    cin>>v[i];
}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll power(ll x, ll y)  
{  
    ll res = 1;        
    while (y > 0)  
    {  
        if (y & 1)  
            res = (res*x)%mod;  
        y = y/2;  
        x = (x*x)%mod;
    }  
    return res%mod;  
}
// solution
ll modinv(ll x)
{
	return power(x,mod-2)%mod;	
}
//Sieve
vector<bool> prime(maxn,true);
void sieve() 
{ 
	ll n=maxn;  
	prime[0]=false;
	prime[1]=false;
    for (ll p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (ll i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
} 

int main() 
{
//   ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
	long long int i,j,inc,dec,t,r;
    long long int c,d,e,temp,m,p,w,k,x,y,sum,pre,temp2,mid,n,low,high,ans;
    t=1;
    n=1;
    //cin>>t;  
    while(t--)
    {
        string s1,s2;
    	cin>>n>>m;
        cin>>s1>>s2;
        vll a[26],b[26];
        for(i=0;i<n;i++)
        {
            a[s1[i]-'a'].pb(i);
        }
        vpll v(m);
        for(i=0,j=0;j<m;i++)
        {
            if(s1[i]==s2[j])
            {
                v[j].ff=i;
                j++;
            }
        }
        for(i=n-1,j=m-1;j>=0;i--)
        {
            if(s1[i]==s2[j])
            {
                v[j].ss=i;
                j--;
            }
        }
        ans=0;
        for(i=1;i<m;i++)
        {
            //d2(v[i].ss,v[i-1].ff);
            ans=max(ans,v[i].ss-v[i-1].ff);
        }
        print_ans;

    }
   return 0;
}
/*
INPUT

*/

