import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

# input=sys.stdin.readline

def resolve():

    n=int(eval(input()))

    s=eval(input())

    dp=[[0]*(n+1) for _ in range(n+1)]

    from itertools import product

    for i,j in product(reversed(list(range(n))),repeat=2):

        if i>=j: continue # i<j

        if s[i]==s[j]:

            dp[i][j]=dp[i+1][j+1]+1

        else:

            dp[i][j]=0

    ans=0

    for i,j in product(reversed(list(range(n))),repeat=2):

        if i>=j: continue

        ans=max(ans,min(j-i,dp[i][j]))

    print(ans)





resolve()
import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=sys.stdin.readline

def resolve():

    def Z(s):

        """

        param s:str

        return list of int with length len(s)

        """

        # initialization

        n=len(s)

        z=[0]*n

        z[0]=n

        # construct

        L,R=0,0

        for i in range(1,n):

            if i>R: # 過去の結果が全く使えない

                L=R=i

                while(R<n and s[R-L]==s[R]): R+=1

                z[i]=R-L

                R-=1 # 0-indexedでiと比較するため

            elif z[i-L]<R-i+1:

                z[i]=z[i-L]

            else:

                L=i

                R+=1 # Rまでは確定しているので、R+1から確認

                while(R<n and s[R-L]==s[R]): R+=1

                z[i]=R-L

                R-=1

        return z



    # input

    n=int(eval(input()))

    s=eval(input())

    ans=0

    for i in range(n):

        z=Z(s[i:])

        k=len(z) # k=n-i

        for j in range(k):

            ans=max(ans,min(j,z[j]))

    print(ans)

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
# input=sys.stdin.readline
def resolve():
    n = int(eval(input()))
    s = eval(input())
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    from itertools import product

    for i, j in product(reversed(list(range(n))), repeat=2):
        if i >= j:
            continue  # i<j
        if s[i] == s[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
        else:
            dp[i][j] = 0
    ans = 0
    for i, j in product(reversed(list(range(n))), repeat=2):
        if i >= j:
            continue
        ans = max(ans, min(j - i, dp[i][j]))
    print(ans)


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = sys.stdin.readline


def resolve():
    def Z(s):
        """
        param s:str
        return list of int with length len(s)
        """
        # initialization
        n = len(s)
        z = [0] * n
        z[0] = n
        # construct
        L, R = 0, 0
        for i in range(1, n):
            if i > R:  # 過去の結果が全く使えない
                L = R = i
                while R < n and s[R - L] == s[R]:
                    R += 1
                z[i] = R - L
                R -= 1  # 0-indexedでiと比較するため
            elif z[i - L] < R - i + 1:
                z[i] = z[i - L]
            else:
                L = i
                R += 1  # Rまでは確定しているので、R+1から確認
                while R < n and s[R - L] == s[R]:
                    R += 1
                z[i] = R - L
                R -= 1
        return z

    # input
    n = int(eval(input()))
    s = eval(input())
    ans = 0
    for i in range(n):
        z = Z(s[i:])
        k = len(z)  # k=n-i
        for j in range(k):
            ans = max(ans, min(j, z[j]))
    print(ans)


resolve()

-# input=sys.stdin.readline +input = sys.stdin.readline + + +    def Z(s): +        """ +        param s:str +        return list of int with length len(s) +        """ +        # initialization +        n = len(s) +        z = [0] * n +        z[0] = n +        # construct +        L, R = 0, 0 +        for i in range(1, n): +            if i > R:  # 過去の結果が全く使えない +                L = R = i +                while R < n and s[R - L] == s[R]: +                    R += 1 +                z[i] = R - L +                R -= 1  # 0-indexedでiと比較するため +            elif z[i - L] < R - i + 1: +                z[i] = z[i - L] +            else: +                L = i +                R += 1  # Rまでは確定しているので、R+1から確認 +                while R < n and s[R - L] == s[R]: +                    R += 1 +                z[i] = R - L +                R -= 1 +        return z + +    # input -    dp = [[0] * (n + 1) for _ in range(n + 1)] -    from itertools import product - -    for i, j in product(reversed(list(range(n))), repeat=2): -        if i >= j: -            continue  # i<j -        if s[i] == s[j]: -            dp[i][j] = dp[i + 1][j + 1] + 1 -        else: -            dp[i][j] = 0 -    for i, j in product(reversed(list(range(n))), repeat=2): -        if i >= j: -            continue -        ans = max(ans, min(j - i, dp[i][j])) +    for i in range(n): +        z = Z(s[i:]) +        k = len(z)  # k=n-i +        for j in range(k): +            ans = max(ans, min(j, z[j]))