#include <bits/stdc++.h>

using namespace std;
#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define _dec(i,N,j) for(int i = (N-1);i >= (j);i--)
#define _rec(i,N,j) for(int i = (N);i >=(j);i--)
#define ALL(x) x.begin(),x.end()
#define MEM(a,n) memset(a,n,sizeof(a))
#define fst first
#define scd second
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

int get_int(){int x;scanf("%d",&x);return x;}
LL get_ll(){LL x;scanf("%lld",&x);return x;}
double get_db(){double x;scanf("%lf",&x);return x;}

template<typename T> int chmin(T &a,T b){if(a > b){a = b;return 1;}else return 0;}
template<typename T> int chmax(T &a,T b){if(a < b){a = b;return 1;}else return 0;}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {_for(i,0,v.size()) os << v[i] << " ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){for(auto a:v) os << a <<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& v){os << v.fst <<" "<<v.scd << " ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>& v){for(auto a:v) cout << a << endl;return os;}

static const int dx[8] = { -1,  0, 1, 0,  1, -1,  1, -1};
static const int dy[8] = {0,  1, 0,  -1,  1, -1, -1,  1};
const double pi  = acos(-1.0);
const double eps = 1e-8;
const LL  INF  = 0x3f3f3f3f3f3f3f3f;
const int inf  = 0x3f3f3f3f;

const int maxn = 5e3 + 5;
const int maxnchar = 256;
char S[maxn];
//SA suffix array , rk rank array, height ht array.
int fir[maxn],sec[maxn],buc[maxn],SA[maxn],rk[maxn],height[maxn],tmp[maxn];
int N;

int ST[maxn][30];


void get_suf(){
    MEM(buc,0);MEM(rk,0);
    int len = strlen(&S[1]);
    N = len;


    _rep(i,1,len) buc[S[i]]++;
    _rep(i,1,256) buc[i]+=buc[i-1];
    _rep(i,1,len) rk[i] = buc[S[i] - 1] + 1;

    for(int k = 1; k < len;k = (k<<1)){
        _rep(i,1,len) fir[i] = rk[i];
        _rep(i,1,len) sec[i] = i + k > len ? 0 : rk[i + k];


        MEM(buc,0);
        _rep(i,1,len) buc[sec[i]]++;
        _rep(i,1,len) buc[i] += buc[i-1];
        _rep(i,1,len) {
            int j = sec[i];
            //cout << buc[j] << endl;
            tmp[len - (--buc[j])] = i;
        }


        MEM(buc,0);
        _rep(i,1,len) buc[fir[i]]++;
        _rep(i,1,len) buc[i]+=buc[i-1];
        _rep(i,1,len) {
            int j = tmp[i];
            SA[buc[fir[j]]--] = j;
        }



        int last = 0;
        int uniq = 1;
        _rep(i,1,len){
            int j = SA[i];
            if(!last){
                rk[j] = 1;
            }else{
                if(fir[j] == fir[last] && sec[j] == sec[last]){
                    uniq = 0;
                    //printf("ok\n");
                    rk[j] = rk[last];
                }else rk[j] = rk[last] + 1;
            }
            last = j;
        }

        if(uniq) break;

    }
}

void get_ht(){
    int k = 0;
    _rep(i,1,N){

        if(rk[i] == 1) k = 0;
        else{
            if(k > 0) k--;
            int j = SA[rk[i] - 1];
            while(i + k <= N && j + k <= N && S[i + k] == S[j + k]) k++;

        }
        height[rk[i]] = k;
    }
}

void test_suf(){
    _rep(i,1,N) printf("  %d",SA[i]);printf("\n");
    _rep(i,1,N) printf("  %d",rk[i]);printf("\n");
     _rep(i,1,N) printf("  %d",height[i]);printf("\n");
}

void initRMQ(){

    _rep(i,1,N){
        ST[i][0] = height[i];
    }

    for(int j = 1;(1<<j) <= N;j++){
        for(int i = 1;i + (1<<j) <= N + 1;i++){
            ST[i][j] = min(ST[i][j-1],ST[i + (1<<(j-1))][ j -1 ]);

        }
    }
    //cout << ST[3][1] << endl;
}

int RMQ(int L,int R){
    int k = 0;
    while( (1<< (k+1)  ) <= R - L + 1) k++;
    return min(ST[L][k],ST[R - (1<<k) + 1][k]);
}

int LCP(int i,int j){

    if(i == j) return N - i + 1;

    if(rk[i] > rk[j]) swap(i,j);

    //cout << rk[i] + 1 <<" "<< rk[j] << endl;

    return RMQ(rk[i] + 1,rk[j]);
}

void init(){
    get_suf();
    get_ht();
    initRMQ();
}


int main()
{
    scanf("%d\n",&N);
    scanf("%s",&S[1]);
    init();
    int ans = 0;
    //test_suf();
    _rep(i,1,N){
        _rep(j,i+1,N){
           //cout << "i: "<<i<<"j: "<<j <<endl;
           int k = LCP(i,j);
           //cout <<"K: "<<k<<endl;
           int a = min(j-i , k);
           ans = max(a,ans);
        }
    }
    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 1024