def main():

    n = int(eval(input()))

    s = eval(input())

    dp = [[0 for i in range(n+1)] for j in range(n+1)]

    

    from itertools import product

    for i,j in product(reversed(list(range(n))),repeat=2):###for を置き換え

        if j>i:

            continue

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

            dp[i][j] = 0 #continueではない	    

        else:

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

           	"""

            これでは結局ループしている

            cnt = 0

            for k in range(min(n-j,j-i)):

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

                    cnt += 1

                else:

                    break

            dp[i][j] = cnt

            """

    mx = 0

    #print(dp)

    for i in range(n):

      	for j in range(i):###

        	mx = max(mx,min(i-j,dp[i][j]))###i-jで抑える

    print(mx)



if __name__ =='__main__':main()

            

            

            

#z-algolithm



n = int(eval(input()))

s_ = eval(input())

ans = 0

for i in range(n-2):

    l = 1 #1から

    r = 1

    s = s_[i:]

    z = [0]*(n-i)

    z[0] = n-i

    while l<n-i:

        while r<n-i and s[r] == s[r-l] : #r<n

            r += 1

        if r == l:

            r+=1;l+=1;continue

        z[l] = r-l

        k = 1

        while l+k<r and z[k]<r-l-k:

            z[l+k] = z[k]

            k+=1

        l = l+k

    for i in range(n-i):###ここで処理

        z[i] = min(z[i],i)

    ans = max(ans,max(z))

print(ans)
def main():
    n = int(eval(input()))
    s = eval(input())
    dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
    from itertools import product

    for i, j in product(reversed(list(range(n))), repeat=2):  ###for を置き換え
        if j > i:
            continue
        if s[i] != s[j]:
            dp[i][j] = 0  # continueではない
        else:
            dp[i][j] = dp[i + 1][j + 1] + 1
            """
            これでは結局ループしている
            cnt = 0
            for k in range(min(n-j,j-i)):
                if s[i+k] ==s[j+k]:
                    cnt += 1
                else:
                    break
            dp[i][j] = cnt
            """
    mx = 0
    # print(dp)
    for i in range(n):
        for j in range(i):  ###
            mx = max(mx, min(i - j, dp[i][j]))  ###i-jで抑える
    print(mx)


if __name__ == "__main__":
    main()

# z-algolithm
n = int(eval(input()))
s_ = eval(input())
ans = 0
for i in range(n - 2):
    l = 1  # 1から
    r = 1
    s = s_[i:]
    z = [0] * (n - i)
    z[0] = n - i
    while l < n - i:
        while r < n - i and s[r] == s[r - l]:  # r<n
            r += 1
        if r == l:
            r += 1
            l += 1
            continue
        z[l] = r - l
        k = 1
        while l + k < r and z[k] < r - l - k:
            z[l + k] = z[k]
            k += 1
        l = l + k
    for i in range(n - i):  ###ここで処理
        z[i] = min(z[i], i)
    ans = max(ans, max(z))
print(ans)

-def main(): -    n = int(eval(input())) -    s = eval(input()) -    dp = [[0 for i in range(n + 1)] for j in range(n + 1)] -    from itertools import product - -    for i, j in product(reversed(list(range(n))), repeat=2):  ###for を置き換え -        if j > i: +# z-algolithm +n = int(eval(input())) +s_ = eval(input()) +ans = 0 +for i in range(n - 2): +    l = 1  # 1から +    r = 1 +    s = s_[i:] +    z = [0] * (n - i) +    z[0] = n - i +    while l < n - i: +        while r < n - i and s[r] == s[r - l]:  # r<n +            r += 1 +        if r == l: +            r += 1 +            l += 1 -        if s[i] != s[j]: -            dp[i][j] = 0  # continueではない -        else: -            dp[i][j] = dp[i + 1][j + 1] + 1 -            """ -            これでは結局ループしている -            cnt = 0 -            for k in range(min(n-j,j-i)): -                if s[i+k] ==s[j+k]: -                    cnt += 1 -                else: -                    break -            dp[i][j] = cnt -            """ -    mx = 0 -    # print(dp) -    for i in range(n): -        for j in range(i):  ### -            mx = max(mx, min(i - j, dp[i][j]))  ###i-jで抑える -    print(mx) - - -if __name__ == "__main__": -    main() +        z[l] = r - l +        k = 1 +        while l + k < r and z[k] < r - l - k: +            z[l + k] = z[k] +            k += 1 +        l = l + k +    for i in range(n - i):  ###ここで処理 +        z[i] = min(z[i], i) +    ans = max(ans, max(z)) +print(ans)