#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define fir first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define ull unsigned long long
#define cl(a, b) memset(a, b, sizeof(a))
#define quickio(a) ios::sync_with_stdio(a)
#define datatest() freopen("data.in", "r", stdin)
#define makeans() freopen("data.out", "w", stdout)
#define makedata() freopen("data.in", "w", stdout)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
using namespace std;
const int maxn = 1e6 + 10;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxblock = sqrt(maxn) + 10;
const double eps = 1e-7;
const ll INF = 1e16;
int n, m;
char s[maxn], t[maxn];
int l[maxn], r[maxn];
int main() {
  scanf("%d %d", &n, &m);
  scanf("%s", s + 1);
  scanf("%s", t + 1);
  int cnts = 1, cntt = 1;
  while (cntt <= m && cnts <= n) {
    if (s[cnts] == t[cntt]) {
      l[cntt] = cnts;
      cntt++;
    }
    cnts++;
  }
  cnts = n;
  cntt = m;
  while (cntt >= 1 && cnts >= 1) {
    if (s[cnts] == t[cntt]) {
      r[cntt] = cnts;
      cntt--;
    }
    cnts--;
  }
  int Max = -1;
  for (int i = 1; i + 1 <= m; i++) {
    Max = max(Max, r[i + 1] - l[i]);
  }
  printf("%d\n", Max);
  return 0;
}