Problem:
Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include<string.h>
char a[10001];
int l,xx;
int QIAN(int n)
{
	if(n==0) return 0;
	n--;
	while(n>=0)
	{
		if(a[n]!='x') return n;
		n--;
	}
	return -1;
}
int HOU(int n)
{
	if(n==l-1) return l-1;
	n++;
	while(n<l)
	{
		if(a[n]!='x') return n;
		n++;
	}
	return -1;
}
int SUM(int n11,int n22,int j)
{
	int n1,n2,k;
	n1=QIAN(n11);n2=HOU(n22);
	if(n1==-1||n2==-1)
	{
		if(n1==-1&&n2==l-1) return n11;
		else if(n1==0&&n2==-1) return l-n22;
		else if(n1==-1&&n2==-1) return n11+l-n22;
	}
	else if(n1==-1&&n2!=-1) {xx=0;return 0;}
	else if(n2==-1&&n1!=-1) {return l-n2+n1;}
	else if(a[n1]!=a[n2]) {xx=0;return 0;}
	else if(n1>=n2) {return n1-n2+SUM(n1,n2,j);}
	k=j-n1-n2+j;
	if(k<=0) k=0-k;
	return k+SUM(n1,n2,j);
}
int main()
{
    int i,x1,x2,x3,j,s,k,n1,n2,k1;
    char x,b[27];
    while(scanf("%s",&a)!=EOF)
    {
    	x1=0;xx=1;k=0;x3=1,x2=1,s;
    	l=strlen(a);
    	if(l==1)
    	{
    		printf("0\n");
    		continue;
		}
    	for(i=0;i<l;i++)
    	{
    		if(a[i]=='x') continue;
    		printf("%c\n",a[i]);
    		s=1;
    		for(j=1;j<=k1;j++)
    		{
    			if(b[j]==a[i]) {x3=0;break;}
			}
			if(x3==0) continue;
			k++;b[k1]=a[i];
    		for(j=i+1;j<=l;j++)
    		{
    			if(a[j]==a[i]) s++;
    		}
			if(s%2==1) {x1++;x=a[i];k=s;}
			if(x1>=2) {x2=0;printf("%d\n",i);break;}
		}
		if(k1==1) 
		{
			i=0;
			while(i>=1)
			{
				if(a[i]==x) break;
				i++;
			}
			if(l-i>i) i=l-i-i;
			else i=i-l+i;
			printf("%d\n",i-1);
			continue;
		}
		if(x2==0) {printf("-1\n");continue;}
		k=k/2+1;i=0;
		while(k>=1)
		{
			if(a[i]==x) k--;
			printf("i=%d\n",i);
			i++;j=i;
		}
		s=SUM(j-1,j-1,j-1);
		if(xx==1) printf("%d\n",s);
		else printf("-1\n");
	}
    return 0;
}