#include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(int i = (N) - 1; i >= 0; i--)
#define rep1(i,N) for(int i = 1; i <= (N) ; i++)
#define repr1(i,N) for(int i = (N) ; i > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it = v.begin(); it != v.end(); it++)
#define vrepr(v,it) for(auto it = v.rbegin(); it != v.rend(); it++)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; using vl = vector<ll>;
using vi = vector<int>; using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
  #include "NyaanDebug.h"
  #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define trca(v,...) do { cerr << #v << " = "; array_out(v , __VA_ARGS__ );} while(0)
#else
  #define trc(...)
  #define trca(...)
  int main(){solve();}
#endif
 
using P = pair<int,int>; using vp = vector<P>;
constexpr int MOD = /**/ 1000000007; //*/ 998244353;
/////////////////

// 使い方
// 文字列のハッシュ列 Roriha s_rori(S);
// 配列のハッシュ列 RollingHash<vector<int>> vrori(v);
template<typename string_t> struct RollingHash{
  using ull = unsigned long long;
  using Pu = pair<ull , ull>;
  string_t& data;
  vector<Pu> hashed , power;
  int size_;
  static Pu basis;
  
  static constexpr ull rmod = (1ull << 61) - 1;
  
  static constexpr ull mul(const ull a,const ull b){
    ull l1 = (uint32_t)a, h1 = a>>32, l2 = (uint32_t)b, h2 = b>>32;
    ull l = l1*l2, m = l1*h2 + l2*h1, h = h1*h2;
    ull ret = (l & rmod) + (l>>61) + (h << 3) + (m >> 29) + (m << 35 >> 3);
    if(ret > rmod) ret = (ret & rmod) + (ret>>61);
    if(ret >= rmod) ret -= rmod;
    return ret;
  }
  static constexpr ull mul_plus(const ull a,const ull b,const ull c){
    ull l1 = (uint32_t)a, h1 = a>>32, l2 = (uint32_t)b, h2 = b>>32;
    ull l = l1*l2, m = l1*h2 + l2*h1, h = h1*h2;
    ull ret = (l & rmod) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + c;
    if(ret > rmod) ret = (ret & rmod) + (ret>>61);
    if(ret >= rmod) ret -= rmod;
    return ret;
  }
  static constexpr ull modpow(ull a,ull b){
    a %= rmod;
    ull r = 1;
    while(b) {
      if(b & 1) r = mul(r , a);
      a = mul(a , a);
      b >>= 1;
    }
    return r;
  }
  static constexpr bool isPrimitive(ull x, vector<ull> &ds) {
    for(ll d : ds)
      if(d != rmod - 1) {
        if(modpow( x, (rmod - 1) / d ) == 1) return false;
      }
    return true;
  }
  static constexpr Pu get_basis(){
    vector<ull> ds = {2,3,5,7,11,13,31,41,61,151,331,1321};
    auto rand_time = chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
    mt19937_64 rng(rand_time);
    ull a = -1 , b = -1;
    do{ a = rng() % rmod; }while(isPrimitive(a , ds) == false);
    do{ b = rng() % rmod; }while(a == b || isPrimitive(b , ds) == false);
    return Pu(a , b);
  }

  // Rolling Hashの構築　O( |S| )
  // basis_flagがtrueの時はbasisを乱数で初期化、falseの時はbでbasisを初期化
  RollingHash(string_t &S) 
  : data(S) , size_((int)S.size()){
    // hashed[i] 部分列[0 , i)のハッシュを記録 hashed[0] = (0,0)
    // power[i] basisのi乗を記録
    hashed.resize(size_ + 1);
    power.resize(size_ + 1);
    power[0] = Pu(1 , 1); hashed[0] = Pu(0 , 0);
    for(int i = 1 ; i <= size_ ; i++ ){
      power[i].fi = mul( power[i - 1].fi , basis.fi);
      power[i].se = mul( power[i - 1].se , basis.se);
      hashed[i].fi = mul_plus( hashed[i - 1].fi , basis.fi , S[i - 1]);
      hashed[i].se = mul_plus( hashed[i - 1].se , basis.se , S[i - 1]);
    }
  }

  // 部分文字列[ l , r )のハッシュを取得 O(1)
  Pu get(const int l , const int r){
    Pu ret;
    ret.fi = rmod - mul_plus( hashed[l].fi , power[r-l].fi , rmod - hashed[r].fi );
    ret.se = rmod - mul_plus( hashed[l].se , power[r-l].se , rmod - hashed[r].se );
    return ret;
  }

  // 別の文字列のハッシュを取得　O( |T| )
  Pu get_hash(string_t &T){
    Pu ret = Pu(0 , 0);
    for(int i = 0 ; i < (int)T.size() ; i++){
      ret.fi = mul_plus(ret.fi , basis.fi , T[i]);
      ret.se = mul_plus(ret.se , basis.se , T[i]);
    }
    return ret;
  }

  // 別の文字列と一致判定 O ( |S| + |T| )
  // 存在するときは最初の文字を、存在しないときは-1を返す
  int find(string_t &T){
    auto t_hash = get_hash(T);
    int t_size = (int)(T.size());
    // |S| < |T|の時はfor文の中に入らないことに留意する
    for(int i = 0 ; i <= size_ - t_size ; i++){
      if(t_hash == get( i , i + t_size ) ) return i;
    }
    return -1;
  }

  // 別の文字列と一致判定 O ( |S| + |T| )
  // 一致する文字列のindexをすべて返す
  vector<int> find_all(string_t &T){
    auto t_hash = get_hash(T);
    int t_size = (int)(T.size());
    vector<int> ret;
    // |S| < |T|の時はfor文の中に入らないことに留意する
    for(int i = 0 ; i <= size_ - t_size ; i++){
      if(t_hash == get( i , i + t_size ) ) ret.push_back(i);
    }
    return ret;
  }

  // Longest Common Prefix(最長共通接頭辞) O( log |S| )
  // Sの[al , ar)とTの[bl , br)の最長共通接頭辞の長さを求める
  // 引数 (文字列Tのハッシュ列 , 文字列Sの開始地点a , 文字列Tの開始地点b)
  // 返り値　最長共通接頭辞の長さ
  int LongestCommonPrefix(RollingHash<string_t> &t , int al , int bl , int ar = -1 , int br = -1){
    if(ar == -1) ar = size_;
    if(br == -1) br = t.size_;
    int ok = 0 , ng = min(ar - al , br - bl)  + 1;
    while(ok + 1 < ng){
      int med = (ok + ng) / 2;
      if( get(al , med + al) == t.get(bl , med + bl) ) ok = med;
      else ng = med;
    }
    return ok;
  }

  // 文字列の大小比較 O( log |S + T| )　
  // Sの[al , ar)とTの[bl , br)の最長共通接頭辞の長さを求める
  // 返り値 1 Sのほうが大きい 0 同じ -1 Tのほうが大きい
  int strcmp(RollingHash<string_t> &t , int al , int bl , int ar = -1 , int br = -1){
    if(ar == -1) ar = size_;
    if(br == -1) br = t.size_;
    int n = LongestCommonPrefix(t , al , bl , ar , br);
    if(al + n == ar)
      return (bl + n == br) ? 0 : 1;
    else if(bl + n == br)
      return -1;
    else return ( data[al + n] < t.data[bl + n] ) ? 1 : -1; 
  }
  
  // 文字列内の最長一致部分文字列を検索 O ( |S| (log|S|)^2 )
  int LongestCommonSubString(){
    auto func = [&](int len) -> bool {
      map < Pu , int > m;
      for(int i = 0 ; i <= size_ - len ; i++){
        if( (m[get(i , i + len)] += 1) != 1) return true;
      }
      return false; 
    };
    int ok = 0 , ng = size_ ;
    while(ok + 1 < ng){
      int med = (ok + ng) / 2;
      if(func(med)) ok = med;
      else ng = med;
    }
    return ok;
  }

};
// ロリハのハッシュの初期化
template<typename T> pair<unsigned long long,unsigned long long> RollingHash<T>::basis = RollingHash<T>::get_basis();
using Roriha = RollingHash<string>;


struct SuffixArray{
    int _size;
    vector<int> sa;
    string &s;
    SuffixArray(string &str):s(str), _size(str.size()){
        sa.resize(_size + 1);
        vector<int> rank(_size + 1), tmp(_size + 1);
        int k;
        // 比較用の関数 ダブリングを用いてiから2k文字とjから2k文字をO(1)で比較する関数
        auto comp = [&](int i, int j){
            if(rank[i] != rank[j]) return rank[i] < rank[j];
            int ri = i + k <= _size ? rank[i + k] : -1;
            int rj = j + k <= _size ? rank[j + k] : -1;
            return ri < rj;
        };
        // 初期化 asciiを用いて1文字の部分列を辞書順ソート
        for(int i = 0; i <= _size; i++){
            sa[i] = i; rank[i] = (i < _size ? str[i] : -1);
        }
        // ダブリング
        for(k = 1; k <= _size; k *= 2){
            // 2k文字でソート
            sort( sa.begin() , sa.end() , comp);
            // 更新
            tmp[sa[0]] = 0;
            for(int i = 1; i <= _size; i++){
                tmp[sa[i]] = tmp[sa[i - 1]] + (comp(sa[i - 1] , sa[i]) ? 1 : 0);
            }
            for(int i = 0; i <= _size; i++) rank[i] = tmp[i];
        }
    }
    // デバッグ用に実装
    void output() {
        for(int i = 0; i < size(); i++) {
            cout << i << ": " << sa[i] << endl;
        }
    }
    // sa.size()と表せると便利なので実装
    int size() const{return _size + 1;}
    // sa[]と表せると便利なのでオーバーロードしておく
    int operator[](int k) const{return sa[k]; }
};

struct LCPArray {
    const SuffixArray &SA;
    vector<int> LCP, rank;
    int max,left,right;
    LCPArray(const SuffixArray &sa) : SA(sa) , max(-1){
        LCP.resize(SA.size()); rank.resize(SA.size());
        // 初期化　rankはsaの逆関数
        for(int i = 0; i < SA.size(); i++) {
            rank[SA[i]] = i;
        }
        LCP[0] = 0; 
        
        // 構築
        for(int i = 0, h = 0; i < SA.size() - 1 ; i++) {
            int j = SA[rank[i] - 1] ; h ? h-- : h;
            // ここで尺取り法に近い手法を使うことでO(N)でLCPの構築をしている
            while( (i > j ? i : j) + h < SA.size() - 1 && SA.s[i + h] == SA.s[j + h] && ++h );
            if( max < (LCP[rank[i] - 1] = h) ){
                max = h; left = SA[ rank[i] - 1 ]; right = i;
                if(left > right){ int hoge = left; left = right; right = hoge;}
            }
        }
    }
    // デバッグ用に実装
    void output() {
        cout << "max : i = " << left+1 << ", j = " << right+1 << ", k = " << max << endl;
        for(int i = 0 ; i < SA.size() ; i++){
          cout << SA.sa[i] <<" " << LCP[i] <<" " << endl;
        }
    }
};


// セグ木
template<typename T,typename F>
struct ST{
 
  int size;
  vector<T> seg;
  const F func;
  const T UNIT;
  
  ST(int N,F func , T UNIT): func(func) , UNIT(UNIT) {
    size = 1;
    while(size < N) size <<= 1;
    seg.assign(2 * size, UNIT);
  }
 
  ST(const vector<T> &v,F func , T UNIT) : func(func) , UNIT(UNIT){
    // initialize
    int N = (int)v.size();
    size = 1;
    while(size < N) size <<= 1;
    seg.assign(2 * size , UNIT);
    // set
    for(int i = 0; i < N; i++){
      seg[i + size] = v[i];
    }
    build();
  }
 
  void set(int k, T x){
    seg[k + size] = x;
  }
 
  void build(){
    for(int k = size-1; k > 0; k--){
      seg[k] = func(seg[2 * k] , seg[2 * k + 1] );
    }
  }
  
  void update(int k, T x){
    k += size; seg[k] = x;
    while(k >>= 1){
      seg[k] = func( seg[2 * k] , seg[2 * k + 1] );
    }
  }
  
  // 半開区間[a,b)へのクエリ
  T query(int a, int b){
    T L = UNIT, R = UNIT;
    for(a+=size,b+=size; a<b; a>>=1,b>>=1){
      if(a & 1) L = func(L,seg[a++]);
      if(b & 1) R = func(seg[--b],R);
    }
    return func(L, R);
  }
 
  T operator[](const int &k) const{
    return seg[k + size];
  }
 
};

void solve(){
  ini(N);
  ins(S);
  SuffixArray sa(S);
  LCPArray lcp(sa);
  int ans = 0;
  //lcp.output();
  auto f = [&](int a,int b){return min(a,b);};
  ST<int , decltype(f)> st(lcp.LCP , f , inf);
  rep(i , N) rep(j , N){
    if(i == j) continue;
    if(lcp.rank[i] > lcp.rank[j]) continue;
    amax( ans , min( abs(i - j) , st.query( lcp.rank[i] , lcp.rank[j] ) ) );
  }
  out(ans);
}

Measured memory_bytes: 384