#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
#define Maxn 5005
#define LL long long
const int mod = 1e9+7;
int n,h[Maxn],p[Maxn];
char s[Maxn];
map<int,int>M;
bool check(int s)
{
    M.clear();
    for(int i=s;i<=n;i++) 
    {
        int t=(h[i]-(LL)h[i-s]*p[s]%mod+mod)%mod;
        int pre=M[t];
        if(pre) 
        {
            if(i-pre>=s) return true; 
        }
        else M[t]=i;
    }
    return false;
}
int main()
{
    scanf("%d",&n);
    scanf("%s",s+1);
    p[0]=1; h[0]=0;
    for(int i=1;i<=n;i++) h[i]=((LL)h[i-1]*29+s[i])%mod,p[i]=(LL)p[i-1]*29%mod;
    int l=0,r=n;
    while(l<r)
    {
        int mid=(l+r+1)>>1;
        if(check(mid))l=mid;
        else r=mid-1;
    }
    printf("%d\n",l);
    return 0;
}