#include <bits/stdc++.h>
//#include <atcoder/all>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define BIT(t) (1ll << (t))
#define PER(i, y, x) for (ll i = y; i >= x; i--)
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define SIZE(v) ll(v.size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
//        ios::sync_with_stdio(false);
//        cin.tie(nullptr);

//using mint = modint1000000007;
//mint::set_mod(998244353);



int main(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    ll n, m; cin >> n >> m;
    string s, t;
    cin >> s >> t;
    vll ml(m), mr(m);
    ll jj = 0;
    REP(i,0,n-1){
        if(s[i] == t[jj]){
            ml[jj] = i;
            jj++;
        }
        if(jj == m)break;
    }
    jj = m-1;
    PER(i,n-1,0){
        if(s[i] == t[jj]){
            mr[jj] = i;
            jj--;
        }
        if(jj == -1){
            break;
        }
    }
    ll ans = -1e9;
    REP(i,0,m-2){
        ans = max(ans, mr[i+1] - ml[i]);
    }
    cout << ans << endl;
}