'''11 4
ababcadacdd
abcd'''
n, m = map(int, input().split())
a1 = input()
b1 = input()
lst1 = []
j = 0
for i in b1:
while j < n:
if a1[j] == i:
lst1.append(j)
break
j += 1
j += 1
lst2 = []
print(lst1)
j = n-1
for i in reversed(b1):
while j > -1:
if a1[j] == i:
lst2.append(j)
break
j -= 1
j -= 1
lst2.reverse()
print(lst2)
if lst1[0] != lst2[0] or lst1[-1] != lst2[-1]:
max1 = 0
for i in range(1, m):
max1 = max(lst2[i-1] - lst1[i], max1)
max2 = 0
for i in range(1, m):
max2 = max(lst2[i] - lst1[i-1], max2)
print(max(max1, max2))
else:
max1 = 0
for i in range(1, m):
max1 = max(lst1[i] - lst1[i-1], max1)
max2 = 0
for i in range(1, m):
max2 = max(lst2[i] - lst2[i-1], max2)
print(max(max1, max2))