#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 10000000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define single_case solve();
#define line cerr<<"----------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); }
#define mod 1000000007LL
const int N = 1e6;
int prefix[N], suffix[N];


void solve()
{
    int n, m;
    string s, t;
    cin>>n>>m>>s>>t;
    for(int i = 0;i<n;i++) prefix[i] = 0, suffix[i] = 0;
    int ind = 0;
    for(int i = 0;i<n;i++)
    {
        if(ind>=m) break;
        if(s[i]==t[ind])
        {
            prefix[ind] = i;
            ind++;
        }
    }
    ind = m-1;
    for(int i = n-1;i>=0;i--)
    {
        if(ind<0) break;
        if(s[i]==t[ind])
        {
            suffix[ind] = i;
            ind--;
        }
    }
    int res = 1;
    for(int i = 0;i<m-1;i++)
    {
        res = max(res, suffix[i+1]-prefix[i]);
    }
    cout<<res;
}


int main()
{
    ios
    single_case





    return 0;
}