This page is still under construction.

Parts of this page are still being built. What you see may change.

Operation Optimization

Time limit2sMemory limit256 MB

Summary
Find the shortest sequence of append-0, append-1, and self-doubling operations whose two-fold application to the empty string yields a given binary string S.
Level

Hard8 of 10

Topics
String, Greedy, Math, Implementation
Solved
No attempts yet

Problem

Hyunwook, who loves puzzle games, has found another interesting puzzle. The player starts with one empty string and can apply the following three operations to it.

  • A: append 0 to the end of the current string.
  • B: append 1 to the end of the current string.
  • C: append a copy of the current string to the end of the current string.

The player can chain these operations in order to form a new operation FF. FF is written as the list of the operations used, in order. For example, F=BAF = BA is the operation that appends 10 to the end of the given string.

The puzzle gives a target string SS. Applying FF twice to the empty string must produce SS. That is, applying FF to the empty string gives some string, and applying FF once more to that string must give SS.

For example, if SS is 100100, then F=BAAF = BAA satisfies the condition. Applying BAABAA to the empty string gives 100, and applying BAABAA to 100 gives 100100.

The shorter FF is, that is, the fewer operations FF uses, the higher the score. Help Hyunwook find the length of the shortest FF that satisfies the condition.

Input

The first line contains the length NN of the target string SS (1≤N≤1061 \le N \le 10^6). The second line contains the target string SS. SS consists only of 0 and 1.

Output

Print the length of the shortest FF that satisfies the condition. If no such FF exists, print -1.

Examples2

  1. Example 1

    Input
    6
    100100
    
    Expected output
    3
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    -1