#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define INF 100000000
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())
#define cpresent(c, x) (find(all(c), x) != c.end())

int main()
{
    ll n,m,l1,l2,maxpos[200005],minpos[200005],i,j;
    char s1[200005],s2[200005];
    cin >> n >> m;

    cin >> s1 >> s2;
    l1 = strlen(s1);
    l2 = strlen(s2);

    i = 0;

    for (j = 0; j < l1; j++)
    {
        if (s2[i] == s1[j])
        {
            minpos[i] = j;
            i++;
        }
    }

    i = m-1;
    for (j = n-1; j > 0; j--)
    {
        if (s2[i] == s1[j])
        {
            maxpos[i] = j;
            i--;
        }
    }

   

    m = 1;
    for(i=1;i<l2;i++){
        ll x = maxpos[i] - minpos[i-1];
        //cout<<x<<endl;
        m = max(m,x);
    }

    cout<<m<<endl;



    return 0;
}