use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;
use std::collections::HashMap;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let s = scan.chars();
    let rolling_hash = RollingHash::new(&s);
    let mut ok = 0;
    let mut ng = n/2 + 1;
    while (ng - ok) > 1 {
        let mid = (ok + ng) / 2;
        let mut is_ok = false;
        'outer: loop {
            let mut hmap = HashMap::new();
            for i in 0..n - mid + 1 {
                let p = rolling_hash.get(i..i+mid);
                if hmap.contains_key(&p) {
                    if i >= mid + hmap[&p] {
                        is_ok = true;
                        break 'outer;
                    }
                } else {
                    hmap.insert(p, i);
                }
            }
            break;
        }
        if is_ok {ok = mid;}
        else {ng = mid;}
    }
    println!("{}", ok);
}


struct RollingHash {
    base1: i64,
    base2: i64,
    mod1: i64,
    mod2: i64,
    hash1: Vec<i64>,
    hash2: Vec<i64>,
    power1: Vec<i64>,
    power2: Vec<i64>
}

impl RollingHash {
    /// O(s.len())
    fn new(s: &[char]) -> RollingHash {
        let base1 = 1007i64;
        let base2 = 2009i64;
        let mod1 = 1_000_000_007i64;
        let mod2 = 1_000_000_009i64;
        let n = s.len();
        let mut hash1 = vec![0; n+1];
        let mut hash2 = vec![0; n+1];
        let mut power1 = vec![1; n+1];
        let mut power2 = vec![1; n+1];
        for i in 0..n {
            hash1[i+1] = (hash1[i]*base1 + s[i] as i64) % mod1;
            hash2[i+1] = (hash2[i]*base2 + s[i] as i64) % mod2;
            power1[i+1] = (power1[i]*base1) % mod1;
            power2[i+1] = (power2[i]*base2) % mod2;
        }
        RollingHash {base1: base1, base2: base2, mod1: mod1, mod2: mod2, hash1: hash1, hash2: hash2, power1: power1, power2: power2}
    }

    /// hash of s[l,r)
    fn get(&self, range: std::ops::Range<usize>) -> (i64, i64) {
        let l = range.start;
        let r = range.end;
        let mut res1 = self.hash1[r] - self.hash1[l] * self.power1[r-l] % self.mod1;
        if res1 < 0 {res1 += self.mod1;}
        let mut res2 = self.hash2[r] - self.hash2[l] * self.power2[r-l] % self.mod2;
        if res2 < 0 {res2 += self.mod2;}
        (res1, res2)
    }

    /// longest common prefix of `s[a,)` and `s[b,)`
    fn lcp(&self, a: usize, b: usize) -> usize {
        let len = std::cmp::min(self.hash1.len()-a, self.hash1.len()-b);
        let mut ok = 0;
        let mut ng = len;
        while ng - ok > 1 {
            let mid = (ok + ng) / 2;
            if self.get(a..a+mid) != self.get(b..b+mid) {
                ng = mid;
            } else {ok = mid;}
        }
        ok
    }
}

// region template
#[allow(dead_code)]
fn iu(i: i64) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> i64 { i as i64 }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion

Measured memory_bytes: 4352