import math

def main():
    n = int(input())
    s = input()
    ans = solve(n, s)
    print(ans)


def solve(n, s):
    ans = 0
    for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
    return ans

def Z(s):
    l = len(s)
    ans = [l for _ in range(l)]

    i, j = 1, 0
    while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
    return ans

if __name__ == '__main__':
    main() import math math def main():
    n = int(input())
    s = input()
    ans = solve(n, s)
    print(ans) main n = int(input()) n int(input()) int input() input s = input() s input() input ans = solve(n, s) ans solve(n, s) solve n s print(ans) print ans def solve(n, s):
    ans = 0
    for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
    return ans solve ans = 0 ans 0 for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
     i range(n - 1) range n - 1 n 1 tmp = Z(s[i:]) tmp Z(s[i:]) Z s[i:] s i: i min(tmp[j], j)for j in range(n - i) for j in range(n - i) j range(n - i) range n - i n i for j in range(n - i) min(tmp[j], j) min tmp[j] tmp j j t = [min(tmp[j], j)for j in range(n - i)] t [min(tmp[j], j)for j in range(n - i)] ans = max(ans, max(t)) ans max(ans, max(t)) max ans max(t) max t return ans ans n n s s def Z(s):
    l = len(s)
    ans = [l for _ in range(l)]

    i, j = 1, 0
    while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
    return ans Z l = len(s) l len(s) len s l for _ in range(l) for _ in range(l) _ range(l) range l for _ in range(l) l ans = [l for _ in range(l)] ans [l for _ in range(l)] i, j = 1, 0 i 1 j 0 while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
     i < l i l while i + j < l and s[j] == s[i + j] :
            j += 1
         i + j < l and s[j] == s[i + j] i + j < l i + j i j l s[j] == s[i + j] s[j] s j s[i + j] s i + j i j j += 1 j 1 ans[i] = j ans[i] ans i j if j == 0:
            i += 1
            continue

         j == 0 j 0 i += 1 i 1 continue k = 1 k 1 while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
         k < j and k + ans[k] < j k < j k j k + ans[k] < j k + ans[k] k ans[k] ans k j ans[i + k] = ans[k] ans[i + k] ans i + k i k ans[k] ans k k += 1 k 1 i += k i k j -= k j k return ans ans s s if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def solve(n, s):
    ans = 0
    for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
    return ans def solve(n, s):
    ans = 0
    for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
    return ans solve def Z(s):
    l = len(s)
    ans = [l for _ in range(l)]

    i, j = 1, 0
    while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
    return ans def Z(s):
    l = len(s)
    ans = [l for _ in range(l)]

    i, j = 1, 0
    while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
    return ans Z def main():
    n = int(input())
    s = input()
    ans = solve(n, s)
    print(ans) def main():
    n = int(input())
    s = input()
    ans = solve(n, s)
    print(ans) main
import math

def main():
    n = int(input())
    s = input()
    ans = solve(n, s)
    print(ans)


def solve(n, s):
    ans = 0
    for i in range(n - 1):
        tmp = Z(s[i:])
        t = [min(tmp[j], j)for j in range(n - i)]
        ans = max(ans, max(t))
        #print(tmp)
    return ans

def Z(s):
    l = len(s)
    ans = [l for _ in range(l)]

    i, j = 1, 0
    while i < l:
        while i + j < l and s[j] == s[i + j] :
            j += 1
        ans[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ans[k] < j:
            ans[i + k] = ans[k]
            k += 1
        i += k
        j -= k
    return ans

if __name__ == '__main__':
    main()