#include <bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long N;
    cin >> N;
    string S;
    cin >> S;
    vector<vector<unsigned long>> dp(N + 1, vector<unsigned long>(N + 1));
    unsigned long ans{0};
    for(unsigned long i = 2; i <= N; ++i)for(unsigned long j = 1; j < i; ++j){
        if(S[i - 1] != S[j - 1])continue;
        ans = max(ans, dp[i][j] = min(i - j, dp[i - 1][j - 1] + 1));
    }
    cout << ans << endl;
    return 0;
}