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


void solve()
{
           ll m,n,a,b,c;
           string s,s1;
           cin>>n>>m;
           cin>>s>>s1;
           vector<int>v,v1;
           int k=n-1;
           for(int i=m-1;i>=0;i--)
           {
                     while(true)
                     {
                               if(s[k]==s1[i])
                               {
                                      v.push_back(k);
                                      k--;
                                      break;
                               }
                               k--;
                     }
           }
           k=0;
           for(int i=0;i<m;i++)
           {
                     while(true)
                     {
                               if(s[k]==s1[i])
                               {
                                      v1.push_back(k);
                                      k++;
                                      break;
                               }
                               k++;
                     }
           }
           reverse(v.begin(),v.end());
           int ans=0;
           for(int i=1;i<v.size();i++)
           ans=max(ans,abs(v[i]-v1[i-1]));

           cout<<ans<<"\n";
}
int main()
{
        solve();
        return 0;
}
