#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define rv(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define P pair<int,int>
using namespace std;
const int M = 231000;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int n,m;
int p[M];
char a[M];
char b[M];
int l[M];
int r[M];
void solve() {
    cin >> n >> m >> a >> b;
    int j = 0;
    fo(i,0,n-1) if(a[i]==b[j]) l[j++] = i;
    j = m-1;
    rv(i,n-1,0) if(a[i]==b[j]) r[j--]=i;
    int ans=0;
    fo(i,0,m-2) ans = max(r[i+1]-l[i],ans);
    cout << ans << endl;
}
int main()
{ 
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve();
    }
    return 0;
}
