#include<bits/stdc++.h>
//#include<tr1/unordered_map>
#pragma GCC optimize ("Ofast")
#define ll long long
#define pll pair<long long,long long>
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define forn(i, n) for(int i=0; i<int(n); i++)
#define Forn(i, n) for(int i=1; i<=int(n); i++)

using namespace std;

const ll p = 998244353;
const ll p2 = 1000000007;
const ll p3 = 0x94878787;
const ll mod = 1000000009;

bool ispr(ll x)
{
    for(ll i = 2; i*i<=x; i++) if(x%i == 0) return 0;
    return 1;
}

ll powe(ll x, ll y)
{
    if(!y) return 1;
    ll ret = powe(x, y>>1);
    ret = ret * ret % mod;
    if(y&1) ret = ret * x % mod;
    return ret;
}
ll inv(ll x){return powe(x, mod-2);}

int n;
string s;
ll pn[5010];
ll invpn[5010];
ll prehash[5010];

ll p2n[5010];
ll invp2n[5010];
ll prehash2[5010];

ll p3n[5010];
ll invp3n[5010];
ll prehash3[5010];

void prep()
{
    pn[0] = invpn[0] = p2n[0] = invp2n[0] = p3n[0] = invp3n[0] = 1;
    ll invp = inv(p);
    ll invp2 = inv(p2);
    ll invp3 = inv(p3);
    Forn(i, 5005)
    {
        pn[i] = pn[i-1] * p % mod;
        invpn[i] = invpn[i-1] * invp % mod;
        p2n[i] = p2n[i-1] * p2 % mod;
        invp2n[i] = invp2n[i-1] * invp2 % mod;
        p3n[i] = p3n[i-1] * p3 % mod;
        invp3n[i] = invp3n[i-1] * invp3 % mod;
    }

    ll now = 0;
    ll now2 = 0;
    ll now3 = 0;
    forn(i, n)
    {
        now = (now + (s[i]-'a') * pn[i]) % mod;
        now2 = (now2 + (s[i]-'a') * p2n[i]) % mod;
        now3 = (now3 + (s[i]-'a') * p3n[i]) % mod;
        //now = (now * p + (s[i] - 'a')) % mod;
        prehash[i] = now;
        prehash2[i] = now2;
        prehash3[i] = now3;
    }
}

ll gethash(int x, int y, int id)
{
    if(id == 1)
    {
        ll ans = prehash[y];
        if(x) ans = (ans + mod - prehash[x-1]) % mod;
        return ans * invpn[x] % mod;
    }
    if(id == 2)
    {
        ll ans = prehash2[y];
        if(x) ans = (ans + mod - prehash2[x-1]) % mod;
        return ans * invp2n[x] % mod;
    }
    if(id == 3)
    {
        ll ans = prehash3[y];
        if(x) ans = (ans + mod - prehash3[x-1]) % mod;
        return ans * invp3n[x] % mod;
    }
}

bool check(int len)
{
    map<ll,int> m;
    map<ll,int> m2;
    map<ll,int> m3;
    for(int i=len-1; i<n; i++)
    {
        ll val1 = gethash(i-len+1, i, 1);
        ll val2 = gethash(i-len+1, i, 2);
        ll val3 = gethash(i-len+1, i, 3);

        if(m.count(val1) && m2.count(val2) && m3.count(val3) )
        {
            if(i - m[val1] >= len) return 1;
        }

        if(!m.count(val1)) m.insert(mp(val1, i));
        if(!m2.count(val2)) m2.insert(mp(val2, i));
        if(!m3.count(val3)) m3.insert(mp(val3, i));
    }
    return 0;
}



int main()
{
    cin.tie(0); ios_base::sync_with_stdio(false);
    cin >> n >> s;
    prep();

    /*forn(i, n)
    {
        cout << i << " " << prehash[i] << endl;
    }
    cout << endl;

    cout << (prehash[6] + mod - prehash[5]) % mod << endl;
    cout << powe(p, 6) << endl;*/

    int lo = 0;
    int hi = n;
    while(lo + 1 != hi)
    {
        int mid = (lo+hi) / 2;
        if(check(mid)) lo = mid;
        else hi = mid;
    }
    cout << lo << endl;
}


Measured memory_bytes: 1536