#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define fi first
#define se second
#define in insert
#define mp make_pair
#define mt make_tuple 
#define loop(i,n) for (int i=0;i<n;i++)
#define Loop(i,n) for (int i=1;i<=n;i++)

const int inf=1e18;
const int mod=1e9+7;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t,res=0,i,n,p,m,prev=0,j;
    cin>>n>>m;
    string str1,str2;
    cin>>str1>>str2;
    int start[m], end[m];
    loop(i,m){
        char curr=str2[i];
        for (j=prev;j<n;j++){
            if(str1[j]==curr){
                prev=j+1;
                start[i]=j;
                break;
            }
        }
    }
    prev=n-1;
    for (i=m-1;i>=0;i--){
        char curr=str2[i];
        for (j=prev;j>=0;j--){
            if(str1[j]==curr){
                prev=j-1;
                end[i]=j;
                break;
            }
        }
    }
    
    // loop(i,m)   cout<<start[i]<<" ";
    // cout<<endl;
    // loop(i,m)   cout<<end[i]<<" ";
    // cout<<endl;
    
    for (i=1;i<m;i++){
        res=max(res,(end[i]-start[i-1]));
    }
    cout<<res<<endl;
}