#include <iostream>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int M = 5000;
char s[M + 5][M + 5], str[M + 5];
int A[M + 5], lcp[M + 5];
bool cmp(int a, int b)
{
	return strcmp(s[a], s[b]) < 0;
}
int main()
{
	int n;
	scanf("%d", &n);
	scanf("%s", str);
	for(int i = 0; i < n; i++)
	{
		for (int j = i; j < n; j++)
			s[i][j - i] = str[j];
	}
	for(int i = 0; i < n; i++) A[i] = i;
	sort(A, A + n, cmp);
	for(int i = 0; i < n - 1; i++)
	{
		int k = 0;
		while(k < n - A[i] && k < n - A[i + 1] && s[A[i]][k] == s[A[i + 1]][k]) k++;
		lcp[i] = k;
	}
	int ans = 0;
	for(int i = 0; i < n - 1; i++)
	{
		int j = i + 1, k = lcp[i];
		while(k && j < n)
		{
			ans = max(ans, min(k, abs(A[i] - A[j])));
			k = min(k, lcp[j]);
			j++;
		}
	}
	cout << ans << endl;
	return 0;
}
