#include <bits/stdc++.h>

using namespace std;

int dp[5001][5001];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    string s;

    cin >> n >> s;

    int ans = 0;

    for(int i = n - 1; i>= 0; i--)
    {
        for(int j = n-1; j > i; j--)
        {
            if(s[i] == s[j])
                dp[i][j] = min(j-i, dp[i+1][j+1] + 1);
            else dp[i][j] = 0;

            ans = max(ans,dp[i][j]);
        }
    }
    cout << ans;

    return 0;
}
