#include<bits/stdc++.h>
using namespace std;
string x,y;
int k,m,n;
int dp1[1001][1001],dp2[1001][1001];
void longCS(){
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
dp2[i][j] = 0;
else if (x[i - 1] == y[j - 1])
dp2[i][j] = dp2[i - 1][j - 1] + 1;
else
dp2[i][j] = max(dp2[i - 1][j], dp2[i][j - 1]);
}
}
}
int f(int i,int j){
if((m-i)<k||(n-j)<k)
return 0;
if(dp1[i][j]!=-1)
return dp1[i][j];
int c=0;
while(i+c<m && j+c<n && x[i+c]==y[j+c])
c++;
int ans = 0;
if(c>=k){
ans = dp2[m-i-c][n-j-c]+c;
//cout<<"val =>"<<i<<","<<(m-i-c)<<" "<<j<<","<<(n-j-c)<<"\n";
//cout<<c<<","<<dp2[m-i-c][n-j-c]<<"\n";
}
return dp1[i][j] = max({ans,f(i+1,j),f(i,j+1)});
}
int main(){
while(cin>>k){
if(k==0) break;
cin>>x>>y;
m = x.length(),n=y.length();
memset(dp1,-1,sizeof(dp1));
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
longCS();
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
cout<<f(0,0)<<"\n";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cmluZyB4LHk7CmludCBrLG0sbjsKaW50IGRwMVsxMDAxXVsxMDAxXSxkcDJbMTAwMV1bMTAwMV07Cgp2b2lkIGxvbmdDUygpewoJZm9yIChpbnQgaSA9IDA7IGkgPD0gbTsgaSsrKSAgeyAgCiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPD0gbjsgaisrKSB7ICAKICAgICAgICBpZiAoaSA9PSAwIHx8IGogPT0gMCkgIAogICAgICAgICAgICBkcDJbaV1bal0gPSAwOyAgCiAgICAgICAgZWxzZSBpZiAoeFtpIC0gMV0gPT0geVtqIC0gMV0pICAKICAgICAgICAgICAgZHAyW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