Pseudoknot

Find the largest t such that the string splits into u v z^R u^R y z with |u|>=t and |z|>=t, or report -1 if no such split exists.

Hard9StringString matchingBinary searchHash mapNo attempts yetTime limit2sMemory limit512 MB

Problem

An RNA strand is written here as a string of uppercase letters. When a strand folds, two reversed pairs of segments can cross each other, and that shape is called a pseudoknot. Finding the pseudoknot with the longest links is one step in the analysis of a new strand, because longer links mean a more stable structure.

A string ww is a pseudoknot if it can be cut into six consecutive parts

w=uvzRuRyzw = u\,v\,z^R\,u^R\,y\,z

where uRu^R and zRz^R are the reversals of uu and zz. Here u1|u| \ge 1 and z1|z| \ge 1, while vv and yy may be empty. The pair (u,uR)(u, u^R) is one link and the pair (z,zR)(z, z^R) is the other.

For example, ICPCINAEROKCPCIFORKOREA is a pseudoknot, with uu = ICPC, vv = IN, zRz^R = AEROK, uRu^R = CPCI, yy = FOR, and zz = KOREA. The string AQQQQRRRRRQQQQQ is not a pseudoknot, because no cut of it has that form.

Figure 1. A pseudoknot (a) and a string that is not a pseudoknot (b)

In ICPCAEROKCPCIKOREA both vv and yy are empty, and the string is still a pseudoknot. Only uu and zz have to be non-empty.

Figure 2. A pseudoknot whose vv and yy are empty

One string can have several pseudoknot structures. ICPCINAEROKCPCIAECIKOREA can be cut as uu = ICPC, vv = IN, zRz^R = AEROK, uRu^R = CPCI, yy = AECI, zz = KOREA, and it can also be cut as uu = IC, vv = PCINAEROKCPCI, zRz^R = AE, uRu^R = CI, yy = KOR, zz = EA. The first cut has the longer links, so it is the one to report.

Figure 3. Two pseudoknot structures for the same string

Input

Your program reads from standard input. The input is one string over the uppercase alphabet, with no whitespace and no line break inside it. The length of the string is between 1 and 200,000.

Output

Your program writes to standard output. Print one line holding the largest integer tt such that the input string ww can be cut as w=uvzRuRyzw = u\,v\,z^R\,u^R\,y\,z with ut|u| \ge t, zt|z| \ge t, v0|v| \ge 0, and y0|y| \ge 0. If no such tt exists, that is, if ww is not a pseudoknot, print -1 instead.