#include<bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ull unsigned long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define endl "\n"
#define pi pair<int,int>
#define  deb1(a)         cout<<a<<"\n";
#define  deb2(a,b)       cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)     cout<<a<<" "<<b<<" "<<c<<"\n";
#define  debp(a)         cout<<a.ff<<" "<<a.ss<<"\n";
#define  debvec(a)      for(auto it: a)cout<<it<<" ";cout<<" "; cout<<endl;
#define  debvp(a)      for(auto it: a)cout<<it.ff<<" "<<it.ss<<endl;
#define  debmap(a)      for(auto it: a)cout<<"{"<<it.ff<<","<<it.ss<<"}, ";newl;
#define  debarr(a,n)    FOR(i,0,n)cout<<a[i]<<" ";cout<<endl;
#define inf ((int)1<<60)
#define mod 1000000007
using namespace std;
//int,char,float,double,bool,string,break,continue,endl,if,else,set,multiset,multimap,unordered_map,unordered_set,cout,cin,printf,scanf
//strlen,strcmp,compar e,auto,while,return,true,false,sizeof,size,abs,INT_MAX,INT_MIN,itr,it,sort,YES,NO

signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast_io
	int test, i, j;
// 	cin >> test;
	test = 1;
	while (test--) {
		int n, m;
		cin >> n >> m;
		string s, t;
		cin >> s >> t;
		int a[m], b[m];
		j = 0;
		for (int i = 0; i < n; ++i) {
			if (s[i] == t[j]) {
				a[j] = i;
				j++;
			}
		}
		j = m - 1;
		for (int i = n - 1; i >= 0 ; i--) {
			if (s[i] == t[j]) {
				b[j] = i;
				j--;
			}
		}
		int ans = 0;
		for (int i = 0; i < m - 1; ++i) {
			ans = max(ans, b[i + 1] - a[i]);
		}
		cout << ans << endl;
	}

}