#include <bits/stdc++.h>
using namespace std;
string a;
string b;
int len;
int f(int n,int m,int k)
{
if(n == 0 || m == 0) {
return 0;
}
if(a[n-1] == b[m-1]) {
if(k == len-1) {
return f(n-1,m-1,k+1)+len; //if the common segment is of length len
}
else if(k >= len) {
return f(n-1,m-1,k+1)+1; //if the common segment is greater than len
}
else { //if the segment has potential of becoming segment of length len
int x = f(n-1,m-1,k+1);
int y = max(f(n-1,m,0),f(n,m-1,0));
return max(x,y);
}
}
else {
return max(f(n-1,m,0),f(n,m-1,0));
}
}
int main()
{
int n;
int m;
int i;
int j;
int k;
while(1) {
cin>>len; //length of minimum segment
if(len == 0) {
break;
}
cin>>a>>b;
n = a.size();
m = b.size();
cout<<f(n,m,0)<<endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cmluZyBhOwpzdHJpbmcgYjsKaW50IGxlbjsKaW50IGYoaW50IG4saW50IG0saW50IGspCnsKICAgIGlmKG4gPT0gMCB8fCBtID09IDApIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGlmKGFbbi0xXSA9PSBiW20tMV0pIHsKICAgICAgICBpZihrID09IGxlbi0xKSB7CiAgICAgICAgICAgIHJldHVybiBmKG4tMSxtLTEsaysxKStsZW47IC8vaWYgdGhlIGNvbW1vbiBzZWdtZW50IGlzIG9mIGxlbmd0aCBsZW4KICAgICAgICB9CiAgICAgICAgZWxzZSBpZihrID49IGxlbikgewogICAgICAgICAgICByZXR1cm4gZihuLTEsbS0xLGsrMSkrMTsgLy9pZiB0aGUgY29tbW9uIHNlZ21lbnQgaXMgZ3JlYXRlciB0aGFuIGxlbgogICAgICAgIH0KICAgICAgICBlbHNlIHsgICAgICAgICAgICAgICAgICAgICAgIC8vaWYgdGhlIHNlZ21lbnQgaGFzIHBvdGVudGlhbCBvZiBiZWNvbWluZyBzZWdtZW50IG9mIGxlbmd0aCBsZW4KICAgICAgICAgICAgaW50IHggPSBmKG4tMSxtLTEsaysxKTsKICAgICAgICAgICAgaW50IHkgPSBtYXgoZihuLTEsbSwwKSxmKG4sbS0xLDApKTsKICAgICAgICAgICAgcmV0dXJuIG1heCh4LHkpOwogICAgICAgIH0KICAgIH0KICAgIGVsc2UgewogICAgICAgIHJldHVybiBtYXgoZihuLTEsbSwwKSxmKG4sbS0xLDApKTsgCiAgICB9Cn0KaW50IG1haW4oKQp7CiAgICBpbnQgbjsKICAgIGludCBtOwogICAgaW50IGk7CiAgICBpbnQgajsKICAgIGludCBrOwoKICAgIHdoaWxlKDEpIHsKICAgICAgICBjaW4+PmxlbjsgLy9sZW5ndGggb2YgbWluaW11bSBzZWdtZW50IAogICAgICAgIGlmKGxlbiA9PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBjaW4+PmE+PmI7CiAgICAgICAgbiA9IGEuc2l6ZSgpOwogICAgICAgIG0gPSBiLnNpemUoKTsKICAgICAgICBjb3V0PDxmKG4sbSwwKTw8ZW5kbDsKICAgIH0KCn0KCg==