#include "bits/stdc++.h"
using namespace std;
#define ll               long long int
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll
 

template<typename T,typename T1>T max_self(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T min_self(T &a,T1 b){if(b<a)a=b;return a;}
 
const long long int INF=1e9;
const int N=0;
const int modn=1e9+7;

 
 
 
void solve(){
int n,m;cin>>n>>m;
 string s,t;
 cin>>s>>t;
 int left[m],right[m];
 int i=0,j=0;
  while(j<m)
   {while(s[i]!=t[j])	
     i++;
     left[j]=i;
     j++,i++;
    }
   i=n-1; j=m-1;
   while(~j)
   {
   	while(s[i]!=t[j])
   	  i--;
   	  right[j]=i;
   	  i--,j--;
	} 
	int ans=0;
	//rep(i,0,m)
	//cout<<left[i]<<" ";
    rep(i,1,m)
    max_self(ans,right[i]-left[i-1]);
	cout<<ans<<"\n"	;
	
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}