def main():
  n = int(input())
  S = 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(input())
  S = 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) main n = int(input()) n int(input()) int input() input S = input() S input() input ans = 0 ans 0 dp = [0]*(n*n) dp [0]*(n*n) [0] 0 n*n 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))
   j range(n-1, 0, -1) range n-1 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))
   i range(j-1, -1, -1) range j-1 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
       j == n-1 j n-1 n 1 dp[i*n+j] = int(S[i] == S[j]) dp[i*n+j] dp i*n+j i*n i n j int(S[i] == S[j]) int S[i] == S[j] S[i] S i S[j] S j if S[i] == S[j]:
          dp[i*n+j] = dp[i*n+j+1+n] + 1
       S[i] == S[j] S[i] S i S[j] S j dp[i*n+j] = dp[i*n+j+1+n] + 1 dp[i*n+j] dp i*n+j i*n i n j dp[i*n+j+1+n] + 1 dp[i*n+j+1+n] dp i*n+j+1+n i*n+j+1 i*n+j i*n i n j 1 n 1 ans = max(ans, min(dp[i*n+j], j-i)) ans max(ans, min(dp[i*n+j], j-i)) max ans min(dp[i*n+j], j-i) min dp[i*n+j] dp i*n+j i*n i n j j-i j i print(ans) print ans if __name__ == "__main__":
  main() __name__ == "__main__" __name__ "__main__" main() main def main():
  n = int(input())
  S = 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) def main():
  n = int(input())
  S = 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) main
def main():
  n = int(input())
  S = 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()