def main():

  n = int(eval(input()))

  S = eval(input())

  ans = 0

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

  for j in range(n-1, 0, -1):

    for i in range(j-1, -1, -1):

      if j == n-1:

        dp[i][j] = int(S[i] == S[j])

      else:

        if S[i] == S[j]:

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

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

  print(ans)

if __name__ == "__main__":

  main()
def main():

  n = int(eval(input()))

  S = eval(input())

  ans = 0

  dp = [0]*(n*n)

  for j in range(n-1, 0, -1):

    for i in range(j-1, -1, -1):

      if j == n-1:

        dp[i*n+j] = int(S[i] == S[j])

      else:

        if S[i] == S[j]:

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

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

  print(ans)

if __name__ == "__main__":

  main()
def main():
    n = int(eval(input()))
    S = eval(input())
    ans = 0
    dp = [[0] * n for _ in range(n)]
    for j in range(n - 1, 0, -1):
        for i in range(j - 1, -1, -1):
            if j == n - 1:
                dp[i][j] = int(S[i] == S[j])
            else:
                if S[i] == S[j]:
                    dp[i][j] = dp[i + 1][j + 1] + 1
            ans = max(ans, min(dp[i][j], j - i))
    print(ans)


if __name__ == "__main__":
    main()

def main():
    n = int(eval(input()))
    S = eval(input())
    ans = 0
    dp = [0] * (n * n)
    for j in range(n - 1, 0, -1):
        for i in range(j - 1, -1, -1):
            if j == n - 1:
                dp[i * n + j] = int(S[i] == S[j])
            else:
                if S[i] == S[j]:
                    dp[i * n + j] = dp[i * n + j + 1 + n] + 1
            ans = max(ans, min(dp[i * n + j], j - i))
    print(ans)


if __name__ == "__main__":
    main()

-    dp = [[0] * n for _ in range(n)] +    dp = [0] * (n * n) -                dp[i][j] = int(S[i] == S[j]) +                dp[i * n + j] = int(S[i] == S[j]) -                    dp[i][j] = dp[i + 1][j + 1] + 1 -            ans = max(ans, min(dp[i][j], j - i)) +                    dp[i * n + j] = dp[i * n + j + 1 + n] + 1 +            ans = max(ans, min(dp[i * n + j], j - i))