//for n>=3 there is a prime number int the range of (n,3*n/2);
// a+b = a^b+2(a&b)
// a!/b! = (b+1)*(b+2)*...*(a-1)*a.
//any number can be represented as a sum of atmost 3 prime numbers/
// euclidian algorithm-->////////
//a%(a-b)=b if b>a/2;

#include<bits/stdc++.h>
#define int int64_t
using namespace std;
typedef   long long ll;
const ll MOD = 1e18 ;
const ll N = 2e5+2;


void solve(){
    int n,m;
    cin>>n>>m;
    string s,t;
    cin>>s>>t;
    vector<vector<int>>v(30);
    vector<int>pref(n+10),suff(n+10);

    for(int i=0;i<n;i++){
        v[s[i]-'a'].push_back(i);
    }

    int num=-1;

    for(int i=0;i<m;i++){
        int c=t[i]-'a';
        int id=upper_bound(v[c].begin(),v[c].end(),num)-v[c].begin();
        if(id>=v[c].size())continue;
        pref[i]=v[c][id];
        num=v[c][id];
    }
    num=n;
    for(int i=m-1;i>=0;i--){
        int c=t[i]-'a';
        int id=lower_bound(v[c].begin(),v[c].end(),num)-v[c].begin()-1;
        if(id>=v[c].size())continue;
        num=v[c][id];
        suff[i]=v[c][id];
    }

    int ans=1;
    for(int i=0;i<m-1;i++){
        ans=max(ans,suff[i+1]-pref[i]);
    }
    cout<<ans;
}





signed main(){
  //  ios_base::sync_with_stdio(false);
   // cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
   while(t--){
         solve();
         cout<<endl;
   }
}
