﻿#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include<unordered_map>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define _rep(i,a,b)  for(int i=a;i>b;--i)
using namespace std;
typedef long long ll;
int n, m;
const int maxn =2e5 + 11;
int a[maxn], b[maxn];
char s[maxn], t[maxn];
int solve() {
	scanf("%d%d", &n, &m);
	scanf("%s%s", s,t);
	//s->n    t->m;
	int last = 0;
	rep(i, 0, m) {
		rep(j, last, n) {
			if (t[i] == s[j]) {
				a[i] = j;
				last = j + 1; break;
			}
		}
	}
	//rep(i, 0, m)  cout << a[i] << "  ";
	//cout << endl;
	last = n - 1;
	int ans = 1;
	for(int i=m-1;i>=1;--i) {
		for(int j=last;j>a[i-1];--j) {
			//cout << "1*";
			if (t[i] == s[j]) {
			//	cout << j << "  ";
				ans = max(ans, j - a[i - 1]);
				last = j - 1;
				break;
			}
		}
	}
	//cout << endl;
	return ans;
}

int main() {
	printf("%d\n", solve());
}