//jay shree ram

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fori(T, N) for (int i = T; i < N; i++)
#define forin(N, T) for (int i = N - 1; i >= T; i--)
#define forj(T, N) for (int j = T; j < N; j++)
#define fork(T, N) for (int k = T; k < N; k++)
#define all(X) X.begin(), X.end()
#define kk cout << endl;
#define PI 3.1415926535897932384626
#define clr(X) memset(X, 0, sizeof(X))
vector<int> phi;
vector<int> spf;
void banao(int n)
{
    phi.resize(n + 1);
    fori(0, n + 1)
        phi[i] = i;
    fork(2, n + 1)
    {
        if (phi[k] == k)
        {
            phi[k] = k - 1;
            for (int i = 2 * k; i <= n; i += k)
            {
                phi[i] = (phi[i] / k) * (k - 1);
            }
        }
    }
}
bool isprime(int n)
{
    if (phi[n] == n - 1)
        return true;
    else
        return false;
}
void pfactor(int n)
{
    spf.resize(n + 1, 1);
    fori(2, n + 1)
        spf[i] = i;
    for (int i = 4; i <= n; i += 2)
        spf[i] = 2;
    for (int i = 3; i * i <= n; i++)
    {
        if (spf[i] == i)
        {
            for (int j = i * i; j <= n; j += i)
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
vector<int> tpf(int x)
{
    vector<int> ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << fixed << setprecision(10);
    int Test = 1;
    //cin >> Test;
    while (Test--)
    {
        int n, m;
        cin >> n >> m;
        string s, p;
        cin >> s >> p;
        int left[m], right[m];
        int j = 0;
        fori(0, n)
        {
            if (j == m)
                break;
            if (s[i] == p[j])
            {
                left[j] = i;
                j++;
            }
        }
        j = m - 1;
        forin(n, 0)
        {
            if (j < 0)
                break;
            if (s[i] == p[j])
            {
                right[j] = i;
                j--;
            }
        }
        int ans = 1;
        fori(0, m - 1)
        {
            ans = max(ans, right[i + 1] - left[i]);
        }
        cout << ans;
    }
}

//jay bajrang bali