#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct SuffixArray{
    vector<int> suffix_array;
    const string s;
    const int n;

    SuffixArray(const string& str) : s(str), n((int)str.size()) {
        suffix_array.resize(n + 1);
        vector<int> rank(n + 1), nxt_rank(n + 1);
        for(int i = 0; i <= n; ++i){
            suffix_array[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }

        for(int k = 1; k <= n; k <<= 1){
            auto comp = [&](int i, int j) -> bool {
                if(rank[i] != rank[j])  return rank[i] < rank[j];
                else{
                    int ni = i + k <= n ? rank[i + k] : -1;
                    int nj = j + k <= n ? rank[j + k] : -1;
                    return ni < nj;
                }
            };
            sort(begin(suffix_array), end(suffix_array), comp);
            
            nxt_rank[suffix_array[0]] = 0;
            for(int i = 1; i <= n; ++i){
                nxt_rank[suffix_array[i]] = nxt_rank[suffix_array[i - 1]]
                                            + comp(suffix_array[i - 1], suffix_array[i]);
            }
            copy(begin(nxt_rank), end(nxt_rank), begin(rank));
        }
    }

    int operator[](int k) const { return suffix_array[k]; }

    int size() const { return n; }

    // s 中に t があるか O(|t|*log|s|)
    bool is_contain(const string& t){
        int lb = 0, ub = n;
        while(ub - lb > 1){
            int mid = (lb + ub) / 2;
            if(s.compare(suffix_array[mid], t.length(), t) < 0) lb = mid;
            else    ub = mid;
        }
        return s.compare(suffix_array[ub], t.length(), t) == 0;
    }

    void output() {
        for(int i = 0; i <= n; ++i){
            cout << i << "(" << suffix_array[i] << "):" << s.substr(suffix_array[i]) << '\n';
        }
    }
};

template<typename T>
struct SparseTable{
    vector<vector<T>> data;
    vector<int> lookup;     // 2 べきの肩

    SparseTable() = default;

    SparseTable(const vector<T> &v){
        int b = 0;
        while((1 << b) <= v.size()) ++b;
        data.assign(b, vector<T>(1 << b));
        for(int i = 0; i < v.size(); ++i)   data[0][i] = v[i];
        for(int i = 1; i < b; ++i){
            for(int j = 0; j + (1 << i) <= (1 << b); ++j){
                data[i][j] = min(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
            }
        }
        lookup.resize(v.size() + 1);
        for(int i = 2; i < lookup.size(); ++i){
            lookup[i] = lookup[i >> 1] + 1;
        }
    }

    // [l, r)
    inline T query(int l, int r){
        int b = lookup[r - l];
        return min(data[b][l], data[b][r - (1 << b)]);
    }
};

// 最長共通接頭辞配列
struct LongestCommonPrefixArray{
    const SuffixArray& SA;
    vector<int> LCP, rank;
    SparseTable<int> table;
    const int n;

    LongestCommonPrefixArray(const SuffixArray& SA) : SA(SA), n(SA.size()) {
        LCP.resize(n + 1);
        rank.resize(n + 1);
        for(int i = 0; i <= n; ++i) rank[SA[i]] = i;
        
        int h = 0;
        LCP[0] = 0;
        for(int i = 0; i < n; ++i){
            // 文字列中での位置 i の接尾辞と、接尾辞配列中でその一つ前の接尾辞の LCP を求める
            int j = SA[rank[i] - 1];

            // 先頭の分をデクリメント
            if(h > 0)   --h;
            for(; j + h < n && i + h < n; ++h){
                if(SA.s[j + h] != SA.s[i + h])  break;
            }
            LCP[rank[i] - 1] = h;
        }

        table = SparseTable(LCP);        
    }

    int get_LCP(int i, int j) {
        if(rank[i] > rank[j])   swap(i, j);
        return table.query(rank[i], rank[j]);
    }

    int operator[](int k) const { return LCP[k]; }

    size_t size() const { return LCP.size(); }

    void output() {
        for(int i = 0; i < size(); ++i){
            cout << i << ':' << LCP[i] << ',' << SA.s.substr(SA[i]) << '\n';
        }
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    string s;
    cin >> n >> s;
    SuffixArray table(s);
    LongestCommonPrefixArray LCP(table);

    int ans = 0;
    for(int i = 0; i + 1 < n; ++i){
        for(int j = i + 1; j < n; ++j){
            chmax(ans, min(LCP.get_LCP(i, j), j - i));
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3900