#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define us unsigned
#define mod 1000000007
int main(){
    int n, m;
    cin >> n >> m;
    string s, t;
    cin >> s >> t;
    stack <int> p;
    queue <int> q;
    for(int i = 0; i  < t.size(); i++){
        p.push(t[i]);
        q.push(t[i]);
    }
    
    vector <int> left (m, 0);
    vector <int> right (m, 0);
    
    int start = 0, end = m-1;
    for(int i = 0; i < s.size(); i++){
        if(!q.empty() && s[i] == q.front()){
            q.pop();
            left[start++] = i;
        }
    }
    
    for(int i = n-1; i >= 0; i--){
        if(!p.empty() && s[i] == p.top()){
            p.pop();
            right[end--] = i;
        }
    }
    
    int ans = 0;
    /*for(int i = 0; i < m; i++){
        cout << left[i] << " ";
    }
    cout << endl;
    
    for(int i = 0; i < m; i++){
        cout << right[i] << " ";
    }
    cout << endl;*/
    
    for(int i = 1; i < m; i++){
        ans = max(ans, right[i]-left[i-1]);
    }
    
    cout << ans << endl;
}