#include <bits/stdc++.h>
using namespace std; 
typedef long long ll; 
typedef vector<int> vi; 
const ll oo = 0x3f3f3f3f3f3f3f3fll; 
#define pb push_back

const int maxn = 5006; 

int n; 
string s; 
int f[maxn][maxn]; 

int main() {
	cin>>n; 
	cin>>s; 
	int ans = 0; 
	for (int i = n-1; i >= 0; --i) 
		for (int j = n-1; j > i; --j) { 
			f[i][j] = (s[i]==s[j]?f[i+1][j+1]+1:0); 
			ans = max(ans, min(f[i][j],j-i)); 
		}
	cout<<ans<<endl; 
}
