#include <iostream>
#include <string>
#include <vector>
#include <stack>

using namespace std;

int main(){
    /* Problem A
    int tt;
    cin >> tt;
    while(tt--){
        long long p;
        long long a;
        long long b;
        long long c;
        cin >> p >> a >> b >> c;
        long long first = (a-p)%a;
        long long second = (b-p)%b;
        long long third = (c-p)%c;
        while(first < 0){
            first += a;
        }
        while(second < 0){
            second +=b;
        }
        while(third < 0){
            third += c;
        }
        long long smallest = min(first, (min(second, third)));
        cout << smallest << endl;
    }
    */
    
    /* Problem B
    int tt;
    cin >> tt;
    while(tt--){
        int n;
        cin >> n;
        
        vector<int> inputs(n);
        vector<int> result(0);
        for(int i = 0; i < n ; i ++){
            cin >> inputs[i];
        }
        
        stack<int> indices;
        indices.push(0);
        for(int i = 1; i < n ; i ++){
            if(inputs[i]>inputs[indices.top()]){
                indices.push(i);
            }
        }
        
        int end = n;
        
        while(!indices.empty()){
            result.insert(result.end(), inputs.begin()+indices.top(), inputs.begin()+end);
            end = indices.top();
            indices.pop();
        }
        for(int i = 0; i < n - 1 ; i ++){
            cout << result[i] << " ";
        }
        cout << result[n-1] << endl;
    }
    */
    int n;
    int m;
    cin >> n >> m;
    string s;
    string t;
    cin >> s;
    cin >> t;
    
    vector<int> minimum(m);
    vector<int> maximum(m);
    
    int curr = 0;
    for(int i = 0; i < n; i ++){
        if(s[i]==t[curr]){
            minimum[curr] = i;
            curr ++;
        }
        
        if(curr == m){
            break;
        }
    }
    
    curr = m-1;
    for(int i = n - 1; i >= 0; i --){
        if(s[i]==t[curr]){
            maximum[curr] = i;
            curr --;
        }
        
        if(curr == -1){
            break;
        }
    }
    
    int veryLeft = minimum[0];
    int veryRight = maximum[m-1];
    
    int leftIndex = 1;
    int rightIndex = m - 2;
    
    if(m == 2){
        cout << veryRight - veryLeft << endl;
        return 0;
    }
    int maxWidth = 0;
    while(rightIndex >= leftIndex){
        if(maximum[leftIndex] - minimum[leftIndex-1]>=maximum[rightIndex+1] - minimum[rightIndex]){
            rightIndex --;
            maxWidth = max(maxWidth, maximum[leftIndex] - minimum[leftIndex-1]);
        }
        else{
            leftIndex++;
            maxWidth = max(maxWidth, maximum[rightIndex+1] - minimum[rightIndex]);
        }
    }
    cout<<maxWidth<<endl;
    return 0;
}
