#include<iostream>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char s[200010];
char t[200010];
int zuiQian[200010]; 
int zuiHou[200010]; 
int main()
{
    int n, m, M = 1;
    scanf("%d%d", &n, &m);
    scanf("%s", s);
    scanf("%s", t);
    int lastT = -1;
    for (int i = 0; i < m; i++)
    {
        while (s[++lastT] != t[i]) //找到s中第一个没有选过的并且与t的下一个相同的字母。
            ;
        zuiQian[i] = lastT;
    }
    lastT = n;
    for (int i = m - 1; i >= 0; i--)
    {
        while (s[--lastT] != t[i])
            ;
        zuiHou[i] = lastT;
    }
    for (int i = 1; i < m; i++)
    {
        M = max(M, zuiHou[i] - zuiQian[i - 1]); 
    }
    printf("%d\n", M);
    return 0;
}

 	    	  	      		 						 		 	