#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>

int main() {
	int32_t n, m;
	std::cin >> n >> m;

	std::string str1, str2;
	std::cin >> str1 >> str2;

	int32_t cur_char = 0;
	int32_t* for_prefix = new int32_t[m + 1];
	for(int32_t i = 0 ;i < n; i++) {
		if(str1[i] == str2[cur_char]) {
			for_prefix[cur_char + 1] = i + 1;
			cur_char++;
			if(cur_char == m)
				break;
		}
	}
	cur_char = m - 1;
	int32_t* for_suffix = new int32_t[m + 1];
	for(int32_t i = n - 1 ;i >= 0; i--) {
		if(str1[i] == str2[cur_char]) {
			for_suffix[m - cur_char] = n - i;
			cur_char--;
			if(cur_char == -1)
				break;
		}
	}

	int32_t answer = 0;
	for(int32_t i = 1; i < m; i++)
		answer = std::max(answer, n - (for_prefix[i] + for_suffix[m - i]) + 1);
	std::cout << answer;
	return 0;
}



















