#include<iostream>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;
char a[Max], b[Max];
int l[Max];
int main()
{
	int n, m;cin >> n >> m;
	for (int i = 1;i <= n;i++) cin >> a[i];
	for (int i = 1;i <= m;i++) cin >> b[i];
	int g = n;
	for (int i = m;i >= 1;i--)
	{
		while (a[g] != b[i])g--;
		l[i] = g;g--;
	}
	g = 0;
	int ans = 0;
	for (int i = 1;i <= m - 1;i++)
	{
		while (b[i] != a[g])g++;
		ans = max(ans, l[i + 1] - g);
		g++;
	}
	cout << ans << endl;
}
