#include<bits/stdc++.h>
using namespace std;

#define BOOST ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rept(it,v) for(auto it=v.begin();it!=v.end();it++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ll long long
#define all(x) x.begin(),x.end()
#define sp(x) setprecision(x)
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pob pop_back
#define F first
#define S second
#define ct continue
#define fill(a,b) memset(a,b,sizeof(a))
#define bs binary_search
#define test int t ; cin>>t; while(t--)

void solve()
{ 
  ll n,m,i,ma,j,left[200009],right[200009];
  string s1,s2;
  cin>>n>>m;
  cin>>s1;
  cin>>s2;
  j = 0;
  for(i=0;i<n;i++)
  {
    if(j == m)
      break;
    if(s2[j] == s1[i])
    {
      left[j] = i;
      j++;
    }
  }
  j = m-1;
  for(i=n-1;i>=0;i--)
  {
    if(j == -1)
      break;
    if(s2[j] == s1[i])
    {
      right[j] = i;
      j--;
    }
  }
  ma = 0;
  for(i=0;i<m-1;i++)
    ma = max(ma,right[i+1]-left[i]);
  cout<<ma;
}

int main()
{
  BOOST
 //test
  {
    solve();
  }
}