#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let s = read::<String>();
    let sa = construct_sa(&s);
    let lcp = construct_lcp(&s, &sa);

    let mut rank = vec![0; n + 1];
    for i in 0..n + 1 {
        rank[sa[i]] = i;
    }
    let mut v = vec![0; n + 1];
    for i in 0..n + 1 {
        v[rank[i]] = lcp[rank[i]] as i64;
    }

    let st = SparseTable::new(&v);
    debug!(sa);
    debug!(lcp);
    let mut ans = 0;
    for i in 0..n + 1 {
        for j in i + 1..n + 1 {
            let mut temp;
            if rank[i] < rank[j] {
                temp = min(j - i, st.rmq(rank[i], rank[j]) as usize);
            } else {
                temp = min(j - i, st.rmq(rank[j], rank[i]) as usize);
            }
            ans = max(temp, ans);
        }
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn compare_sa(i: usize, j: usize, k: usize, n: usize, rank: &Vec<i32>) -> bool {
    if rank[i] != rank[j] {
        return rank[i] < rank[j];
    }
    let ri = if i + k <= n { rank[i + k] } else { -1 };
    let rj = if i + k <= n { rank[j + k] } else { -1 };
    ri < rj
}

fn construct_sa(s: &str) -> Vec<usize> {
    let n = s.len();
    let s = s.chars().collect::<Vec<_>>();
    let mut sa = vec![0; n + 1];

    let mut rank = vec![0; n + 1];
    for i in 0..n + 1 {
        sa[i] = i;
        rank[i] = if i < n { s[i] as u64 as i32 } else { -1 };
    }

    let mut k: usize = 1;
    while k <= n {
        let mut tmp = vec![0; n + 1];
        sa.sort_by_key(|&i| (rank[i], *rank.get(i + k).unwrap_or(&-1)));
        tmp[sa[0]] = 0;
        for i in 1..n + 1 {
            tmp[sa[i]] = tmp[sa[i - 1]];
            if compare_sa(sa[i - 1], sa[i], k, n, &rank) {
                tmp[sa[i]] += 1;
            }
        }
        rank = tmp;
        k *= 2;
    }
    sa
}

fn construct_lcp(s: &str, sa: &[usize]) -> Vec<usize> {
    let s = s.chars().collect::<Vec<_>>();
    let mut rank = vec![0; s.len() + 1];
    for i in 0..s.len() + 1 {
        rank[sa[i]] = i;
    }

    let mut h = 0;
    let mut lcp = vec![0; s.len() + 1];
    for i in 0..s.len() {
        let mut j = sa[rank[i] - 1];
        if h > 0 {
            h -= 1;
        }
        while j + h < s.len() && i + h < s.len() {
            if s[i + h] != s[j + h] {
                break;
            }
            h += 1;
        }

        lcp[rank[i] - 1] = h;
    }
    lcp
}

struct SparseTable {
    st: Vec<Vec<i64>>,
    lookup: Vec<usize>,
}

impl SparseTable {
    fn new(v: &Vec<i64>) -> SparseTable {
        let mut b = 0;
        while (1 << b) <= v.len() {
            b += 1;
        }
        let mut st = vec![vec![0; 1 << b]; b];
        for i in 0..v.len() {
            st[0][i] = v[i];
        }
        for i in 1..b {
            let mut j = 0;
            while j + (1 << i) <= (1 << b) {
                st[i][j] = std::cmp::min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
                j += 1;
            }
        }
        let mut lookup = vec![0; v.len() + 1];
        for i in 2..lookup.len() {
            lookup[i] = lookup[i >> 1] + 1;
        }
        SparseTable {
            st: st,
            lookup: lookup,
        }
    }
    // [l, r)
    fn rmq(&self, l: usize, r: usize) -> i64 {
        let b = self.lookup[r - l];
        std::cmp::min(self.st[b][l], self.st[b][r - (1 << b)])
    }
}


Measured memory_bytes: 4476