#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(false); cin.tie(0)
#define open() freopen("input.txt", "r",stdin) ; freopen("output.txt","w" ,stdout)
using namespace std;
typedef long long ll;


void smashit() {

	int n, m;
	cin >> n >> m;
	string s, t;
	cin >> s >> t;
	vector<int>left_most(m), right_most(m);
	int j = 0;
	for (int i = 0; i < n; i++) {
		if (j == m)break;
		if (s[i] == t[j]) {
			left_most[j] = i;
			j++;
		}
	}
	j = m - 1;
	for (int i = n - 1; i >= 0; i--) {
		if (j < 0)break;
		if (s[i] == t[j]) {
			right_most[j] = i;
			j--;
		}
	}
	int mx = INT_MIN;
	for (int i = 0; i < m - 1; i++) {
		mx = max(mx, (right_most[i + 1] - left_most[i]));
	}
	cout << mx << "\n";

}



int main()
{
	fastio();

	clock_t start = clock();


	int t = 1;
	// cin>>t;
	while (t--)
	{
		smashit();
	}
	clock_t stop = clock();
	cerr << double(stop - start) / CLOCKS_PER_SEC << '\n';
	return 0;
}
