#include<bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
int f[N], n;
char str[N];
#define bug(x) cout << #x << '=' << x << " "
#define debug(x) cout << #x << "=" << x << endl
void build(int x){// x - n
	f[x] = x, f[x + 1] = x;
	for(int j = x + 2, i = x; j <= n; ++j){
		for(;i != x && str[i + 1] != str[j];)i = f[i];
		if(str[i + 1] == str[j]) ++i;
		f[j] = i;
	}
}
int main(){
	cin >> n;
	scanf("%s", str + 1);
	int ans = 0;
	for(int i = 2; i <= n; ++i){
		build(i);
		int w = i;
		for(int j = 1; j < i; ++j){// i - w : len = w - i + 1
			for(;w != i && str[j] != str[w + 1];)w = f[w];
			if(str[w + 1] == str[j]) ++w;
			if(str[w] == str[j])ans = max(ans, w - i + 1);
		}
	}
	cout << ans << endl;
	return 0;
}