#include <bits/stdc++.h>

#define int long long
#define all(x) x.begin(), x.end()

using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    string s, t;
    cin >> s >> t;
    vector<int> left, right;
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == t[j]) {
            left.push_back(i);
            j++;
            if (j >= m) {
                break;
            }
        }
    }
    j = m - 1;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == t[j]) {
            right.push_back(i);
            j--;
            if (j < 0) {
                break;
            }
        }
    }
    reverse(all(right));
    int res = 0;
    for (int i = 0; i < m - 1; i++) {
        res = max(res, right[i + 1] - left[i]);
    }
    cout << res << '\n';
}