#include <bits/stdc++.h>
#include<string>
#define ll long long
#define mod 1000000007
using namespace std;
  
ll
gcd (ll x, ll y)
{
  if (!y)
    return x;
  return gcd (y, x % y);
}



 
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    
    ll t;
    //cin >> t;
    t=1;
    while(t--){
   ll n,m;
   cin>>n>>m;
   string s,t;
   cin>>s>>t;
   ll first[m+1];
   ll p1,p2;
     memset(first,INT_MAX,sizeof(first));
     ll last[m+1];
     memset(last,-1,sizeof(last));
    
      ll j=0;
      for(ll i=0;i<n;i++){
         if(j==m)
         break;
        if(s[i]==t[j])
        {
       
          
          first[j]=i;
          j++;
        }
      }
        j=m-1;
      for(ll i=n-1;i>=0;i--){
        if(j<0)
        break;
        if(s[i]==t[j])
        {
          
         
          last[j]=i;
          j--;
        }
      }
      ll ans=0;
      for(ll i=0;i<m-1;i++){
        ans=max(ans,last[i+1]-first[i]);
      }
    cout<<ans<<endl;
     
    }
    
 
    return 0;
}