#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;
    }

}

