Jumbled password

Given a string, find the smallest index past the midpoint where the suffix differs from the prefix of equal length in exactly one character.

Medium6StringString matchingHash mapBinary searchNo attempts yetTime limit0.5sMemory limit1024 MB

Problem

Alice is writing down a secret password as she hears it. The sender reads the password a second time, so she keeps writing to check her copy for mistakes. She gets one letter wrong, and her attention slips for a moment, so she misses the end of the stream. Now she cannot tell where the second reading begins.

You are given a string S[0n1]S[0 \ldots n-1] of lowercase English letters. Find the earliest position where the repeat of the password can start. Formally, find the index ii that satisfies all of the following conditions:

  • n2i<n\frac{n}{2} \le i < n
  • S[in1]S[i \ldots n-1] and S[0ni1]S[0 \ldots n-i-1] differ in exactly one position
  • among all indices satisfying the two conditions above, ii is the smallest

Input

The first line contains the length of the string, nn (2n5000002 \le n \le 500\,000).

The second line contains the string SS of nn lowercase English letters.

Output

Print the index ii that satisfies the conditions on a single line. If no such index exists, print 1-1 instead.

Hint

In the first example the two compared strings are abaa and aaaa.