// Slower C++ solution:
// In the name of God



#include <bits/stdc++.h>



using namespace std;



const int N = 5*1e3+5;



int n;

string s;

long long hs[N], p = 37, mod = 1e9+7, pp[N];

long long hs2[N], p2 = 31, pp2[N];



long long fast_pow(long long x, long long y) {

	long long ret = 1;

	for(;y;y>>=1) {

		if(y&1) ret=ret*x%mod;

		x=x*x%mod;

	}

	return ret;

}



long long givehs(int l, int r) {

	long long ret = hs[r];

	if(l) ret = (ret-hs[l-1]+mod)%mod;

	long long div = pp[n-1-r];

	return ret*div%mod;

}



long long givehs2(int l, int r) {

	long long ret = hs2[r];

	if(l) ret = (ret-hs2[l-1]+mod)%mod;

	long long div = pp2[n-1-r];

	return ret*div%mod;

}





bool check(int len) {

	map<pair<long long, long long>, pair<int, int> > mp;

	if(len==0) return true;

	for(int i=0;i<n;i++) {

		if(i+len-1 >= n) break;

		long long now = givehs(i, i+len-1);

		long long now2 = givehs2(i, i+len-1);

		pair<long long, long long> go = make_pair(now, now2);

		pair<int, int> x = make_pair(1e7, -1);

		if(mp.count(go)) x = mp[go];

		x.first = min(x.first, i);

		x.second = max(x.second, i);

		mp[go] = x;

	}

	for(auto x : mp) {

		if(x.second.first+len-1<x.second.second) return true;

	}

	return false;

}





int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cin >> n;

	cin >> s;

	pp[0] = 1;

	pp2[0] = 1;

	for(int i=1;i<N;i++) {

		pp[i] = pp[i-1]*p%mod;

		pp2[i] = pp2[i-1]*p2%mod;

	}

	long long pw = p;

	hs[0] = s[0] - 'a' + 1;

	for(int i=1;i<n;i++) {

		hs[i] = (hs[i-1] + (s[i]-'a'+1)*pw%mod)%mod;

		pw = pw*p%mod;

	}

	pw = p2;

	hs2[0] = s[0] - 'a' + 1;

	for(int i=1;i<n;i++) {

		hs2[i] = (hs2[i-1]+(s[i]-'a'+1)*pw%mod)%mod;

		pw=pw*p2%mod;

	}

	int l = 0, r = n;

	int ans = 0;

	while(l <= r) {

		int m = (l+r) / 2;

		if(check(m)) {

			ans = m;

			l = m + 1;

		}

		else r = m-1;

	}

	cout << ans;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.104690849885914x):
#include <bits/stdc++.h>

using namespace std;



#define LL long long



const int noun = 3;

const LL p1[3] = {127, 769, 1009};

const LL p2[3] = {11618141, 11618107, 11618143};



string S;

int N;

LL P[500010][3], H[500010][3];

bool Hash[11620000][3];



bool check(int m){

	bool flag = false;

	for (int i = m + 1; i + m - 1 <= N; i++){

		LL x;

		for (int j = 0; j < noun; j++){

			x = (P[i - 1][j] - P[i - m - 1][j] * H[m][j] % p2[j] + p2[j]) % p2[j];

			Hash[x][j] = true;

		}

		bool ff = true;

		for (int j = 0; j < noun; j++){

			x = (P[i + m - 1][j] - P[i - 1][j] * H[m][j] % p2[j] + p2[j]) % p2[j];

			if (!Hash[x][j])

				ff = false;

		}

		if (ff){

			flag = true;

			break;

		}

	}

	for (int i = 1; i + m - 1 <= N; i++){

		LL x;

		for (int j = 0; j < noun; j++){

			x = (P[i + m - 1][j] - P[i - 1][j] * H[m][j] % p2[j] + p2[j]) % p2[j];

			Hash[x][j] = 0;

		}

	}

	return flag;

}



int main(){

	cin >> N >> S;

	int l = 0, r = N;

	P[0][0] = P[0][1] = P[0][2] = 0;

	H[0][0] = H[0][1] = H[0][2] = 1;

	for (int i = 1; i <= N; i++)

		for (int j = 0; j < noun; j++){

			P[i][j] = (P[i - 1][j] * p1[j] % p2[j] + S[i - 1] - 'a') % p2[j];

			H[i][j] = H[i - 1][j] * p1[j] % p2[j];

		}

	while (l < r){

		int mid = (l + r + 1) >> 1;

		if (check(mid)) l = mid;

		else r = mid - 1;

	}

	cout << l << endl;

}