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 → xa x bxa → xaxbxa x       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 → x oxxx → x xoxxx → x xxoxxx

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    s = S()
    d = deque(s)
    ans = 0

    

    while len(d) > 1:
        if d[0] == d[-1]:
            d.popleft()
            d.pop()
        elif d[0] == 'x':
            d.append('x')
            ans += 1
        elif d[-1] == 'x':
            d.appendleft('x')
            ans += 1
        else:
            print(-1)
            return

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
N = len(S)
r = 0
l = 0
ans = 0
while l+r < N:
    if S[l] == S[N-1 - r]:
        l += 1
        r += 1
    else:
        if S[l] == 'x':
            l += 1
            ans += 1
        elif S[N-1 - r] == 'x':
            r += 1
            ans += 1
        else:
            print(-1)
            exit()

print(ans)

Solution (Python):



S = input()
N = len(S)

ss = ""
for s in S:
    if s == 'x':
        pass
    else:
        ss += s
if ss != ss[::-1]:
    print(-1)
    exit()
if ss == "":
    print(0)
    exit()


n = len(ss)
if n % 2 == 0:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:
                cnt = j+1
                break
        if i == n//2-1:
            left_center = cnt-1
    right_center = cnt-1
    
    ans = (abs(right_center - left_center) % 2)
    
    count = 0
    x_left = []
    x_right = []
    for i in range(left_center):
        if S[left_center-1 - i] == 'x':
            count += 1
        else:
            x_left.append(count)
            count = 0
    x_left.append(count)
    count = 0
    for i in range(right_center+1, N, 1):
        if S[i] == 'x':
            count += 1
        else:
            x_right.append(count)
            count = 0
    x_right.append(count)
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i] - x_right[i])
    print(ans)
else:
    cnt = 0
    for i in range(n//2+1):
        for j in range(cnt, N):
            if S[j] == "x":
                continue
            else:       
                cnt = j + 1
                break
    center = cnt-1
    
    
    distans = 0     
    count = 0
    x_left = []
    x_right = []
    for i in range(center):
        if S[center-1 - i] == "x":
            count += 1
        else:
            x_left.append([distans, count])
            distans += 1
            count = 0
    x_left.append([distans, count])
    distans = 0
    count = 0
    for i in range(center+1, N):
        if S[i] == "x":
            count += 1
        else:
            x_right.append([distans, count])
            distans += 1
            count = 0
    x_right.append([distans, count])
    
    
    ans = 0
    for i in range(len(x_left)):
        ans += abs(x_left[i][1] - x_right[i][1])
    print(ans)

Solution (Python):
s = input()

l = 0
r = len(s) - 1

ans = 0
while r > l:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == "x":
        l += 1
        ans += 1
    elif s[r] == "x":
        r -= 1
        ans += 1
    else:
        ans = -1
        break

print(ans)

Solution (Python):
s = input()

l = 0
r = len(s) - 1

ans = 0
while r > l:
    if s[l] == s[r] == "x":
        l += 1
        r -= 1
    elif s[l] == "x":
        l += 1
        ans += 1
    elif s[r] == "x":
        r -= 1
        ans += 1
    elif s[l] == s[r]:
        l += 1
        r -= 1
    else:
        ans = -1
        break

print(ans)

Solution (Python):
s = input()

ss = ""
cnt = 0
for e in s:
    if e == "x":
        cnt += 1
    else:
        ss += e

if ss == ss[::-1]:
    l = 0
    r = len(s) - 1
    ans = 0
    while r > l:
        if s[l] == s[r] == "x":
            l += 1
            r -= 1
        elif s[l] == "x":
            l += 1
            ans += 1
        elif s[r] == "x":
            r -= 1
            ans += 1
        else: 
            l += 1
            r -= 1

else:
    ans = -1

print(ans)

Solution (Python):
s=input()
a=l=0
r=len(s)-1
while l<r:
  if s[l]=="x":
    if s[r]=="x":
      l+=1
      r-=1
    else:
      a+=1
      l+=1
  else:
    if s[r]=="x":
      a+=1
      r-=1
    else:
      if s[l]==s[r]:
        l+=1
        r-=1
      else:
        print(-1)
        exit()
print(a)

Solution (Python):
s = input()
l = 0
r = len(s)-1

x = 0
while l < r:
  if s[l] == s[r]:
    l+=1
    r-=1
  elif s[l] == "x":
    x += 1
    l += 1
  elif s[r] == "x":
    x += 1
    r -= 1
  else:
    print(-1)
    break
else:
  print(x)

Solution (Python):
import collections

s = input()
a = collections.deque(s)
ans = 0
while 1 < len(a):
	if a[0] == a[len(a) - 1]:
		a.popleft()
		a.pop()
	elif a[0] == 'x':
		a.append('x')
		ans += 1
	elif a[len(a) - 1] == 'x':
		a.appendleft('x')
		ans += 1
	else:
		ans = -1
		break
print(ans)

Solution (Python):
from collections import deque


def main():
    string = deque(input())
    answer = 0
    while string:
        if string[0] == string[-1]:
            string.pop()
            if string:
                string.popleft()
            answer += 0
        else:
            if string[0] == "x":
                string.append("x")
                answer += 1
            elif string[-1] == "x":
                string.appendleft("x")
                answer += 1
            else:
                answer = -1
                break
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = input()

    l = 0
    r = len(s) - 1

    ret = 0
    while l < r:
        lc, rc = s[l], s[r]

        if lc == rc:
            l += 1
            r -= 1
            continue

        if lc == 'x':
            ret += 1
            l += 1
            continue

        if rc == 'x':
            ret += 1
            r -= 1
            continue

        print(-1)
        return

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
s=input()
li=list(s)

i=0
j=len(s)-1
answer=0

while i<j:
    if li[i]==li[j]:
        i+=1
        j-=1
    else:
        if li[i]!="x" and li[j]!="x":
            answer=-1
            break
        elif li[i]=="x" and li[j]!="x":
            answer+=1
            i+=1
        elif li[i]!="x" and li[j]=="x":
            answer+=1
            j-=1

print(answer)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


s = S()
no_x = ''.join(s.split('x'))
if no_x[::-1] != no_x:
    print(-1)
else:
    L = []
    cnt = 0
    if not no_x:
        print(0)
        exit()
    for i in s:
        if i == 'x':
            cnt += 1
        else:
            L += [cnt]
            cnt = 0
    L += [cnt]
    ans = 0
    for i in range(len(no_x) + 1):
        ans += abs(L[i] - L[len(no_x) - i])
    print(ans // 2)

Solution (Python):
s = input()
S = []
xid = [0] * (10 ** 5 + 1)
xcnt = 0
for c in s:
    if c != "x":
        S.append(c)
        xcnt += 1
    else:
        xid[xcnt] += 1
xid = xid[:xcnt + 1]
ans = 0
if S[0:xcnt // 2] == S[-1:-(xcnt // 2) - 1:-1]:
    for i in range(-(-xcnt // 2)):
        ad = xid[i] - xid[xcnt - i]
        if ad < 0:
            ad = -ad
        ans += ad
    print(ans)
else:
    print(-1)

Solution (Python):
s = input()
sr = s[-1::-1]
left = 0
right = 0
ans = 0
while (1):
    if left + right >= len(s) - 1:
        break
    if s[left] != sr[right]:
        if s[left] == 'x':
            left += 1
        elif sr[right] == 'x':
            right += 1
        else:
            print(-1)
            exit()
        ans += 1
    elif s[left] == sr[right]:
        left += 1
        right += 1

print(ans)

Solution (Python):
S = input()
head, tail, ans = 0, len(S)-1, 0

while head < tail:
    if S[head] == S[tail]:
        head += 1
        tail -= 1
    elif S[head] == 'x':
        head += 1
        ans += 1
    elif S[tail] == 'x':
        tail -= 1
        ans += 1
    else:
        print(-1)
        exit()
print(ans)

Solution (Python):
s = list(input())
s1=s[::-1]
ans = 0
cnt = 0
cnt1 = 0
while cnt<len(s) and cnt1<len(s) and cnt<=len(s)-1-cnt1:
  if s[cnt]!=s1[cnt1]:
    if s[cnt]=="x" and s1[cnt1]!="x":
      cnt+=1
      ans+=1
    elif s1[cnt1]=="x" and s[cnt]!="x":
      cnt1+=1
      ans+=1
    elif s[cnt]!="x" and s1[cnt1]!="x":
      print(-1)
      exit()
  elif s[cnt]==s1[cnt1]:
    cnt+=1
    cnt1+=1
print(ans)

Solution (Python):
import collections

s = input()
a = collections.deque(s)
ans = 0
while 1 < len(a):
	if a[0] != a[len(a) - 1]:
		ans += 1
		if a[0] != 'x':
			a.appendleft('x')
			if a[len(a) - 1] != 'x':
				ans = -1
				break
		else:
			a.append('x')
	else:
		a.popleft()
		a.pop()
print(ans)

Solution (Python):
S = list(input())
ans = 0

fst = 0
lst = len(S)-1
flg = 1

while(1):
    if fst>=lst:
        break

    if S[fst]==S[lst]:
        fst += 1
        lst -= 1
        continue
    
    elif S[fst]=="x":
        ans += 1
        fst += 1
    
    elif S[lst]=="x":
        ans += 1
        lst -= 1
    
    else:
        flg = 0
        break

if flg:
    print(ans)
else:
    print(-1)

Solution (Python):
def main():
    s = input().rstrip()
    ans = 0
    f = True
    l, r = 0, len(s)-1
    while l < r:
        if s[l] != s[r]:
            if s[l] != "x" and s[r] != "x":
                f = False
                break
            elif s[l] == "x":
                ans += 1
                l += 1
            else:
                ans += 1
                r -= 1
        else:
            l += 1
            r -= 1
    if f:
        print(ans)
    else:
        print("-1")

if __name__ == "__main__":
    main()

Solution (Python):
s=input()
l=0
r=len(s)-1
x=0
while l<r:
  if s[l]==s[r]:
    l+=1
    r-=1
  elif s[l]=='x':
    x+=1
    l+=1
  elif s[r]=='x':
    x+=1
    r-=1
  else:
    x=-1
    break
print(x)

Solution (Python):
S = input()
S = list(S)
S_no_x = []
def is_kaibun(kaibun):
    for i in range(len(kaibun)//2):
        if kaibun[i] != kaibun[-i-1]:
            return False
    return True
inds = []
for i,c in enumerate(S):
    if c != "x":
        S_no_x.append(c)
        inds.append(i)
if not is_kaibun(S_no_x):
    print(-1)
else:
    l = len(S_no_x)

    if l == 0:
        print(0)
    else:
        mae = ""
        usiro = ""
        if l % 2 == 0:
            mae_ind = inds[l // 2 - 1]
            usiro_ind = inds[l // 2]
            mae = S[:mae_ind +1]
            usiro = S[usiro_ind :]
        else:
            center = inds[l // 2]
            mae = S[:center]
            usiro = S[center +1:]

        usiro.reverse()
        usiro_x = []
        mae_x = []
        cot = 0
        usiro.append("@")
        for c in usiro:
            if c == "x":
                cot += 1
            else:
                usiro_x.append(cot)
                cot = 0
        cot = 0
        mae.append("@")
        for c in mae:
            if c == "x":
                cot += 1
            else:
                mae_x.append(cot)
                cot = 0
        ans = 0
        for a,b in zip(mae_x,usiro_x):
            ans += abs(a-b)
        print(ans)

Solution (Python):
def main():
    S = input()
    from collections import deque
    S = deque(S)
    ans = 0
    while S:
        if S[0] == S[-1]:
            S.popleft()
            if S:
                S.pop()
        elif S[0] != "x" and S[-1] != "x":
            return print(-1)
        elif S[0] == "x":
            S.append("x")
            ans += 1
        else:
            S.appendleft("x")
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s=input()
    l=len(s)
    i,j=0,l-1
    ans,cnti,cntj=0,0,0
    while i+1<j:
        if s[i]=='x':
            i+=1
            cnti+=1
        elif s[j]=='x':
            j-=1
            cntj+=1
        else:
            if s[i]==s[j]:
                ans+=abs(cnti-cntj)
                cnti,cntj=0,0
                i+=1
                j-=1
            else:
                print(-1)
                exit()
    if i==j:
        print(ans)
    else:
        if s[i]==s[j]:
            print(ans)
        else:
            if s[i]=='x':
                print(ans+abs(cnti+1-cntj))  
            elif s[j]=='x':
                print(ans+abs(cnti-cntj-1))
            else:
                print(-1)          
    
if __name__=='__main__':
    main()

Solution (Python):
s = input()

l, r = 0, len(s)-1
ans = 0
while l < r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == "x":
        l += 1
        ans += 1
    elif s[r] == "x":
        r -= 1
        ans += 1
    else:
        print(-1)
        exit()

print(ans)

Solution (Python):
s=input()
left=0
right=len(s)-1
cnt=0
while right-left>0:
  if (s[right]==s[left]):
    right-=1
    left+=1
  elif (s[right]!='x')and(s[left]!='x'):
    print(-1)
    exit()
  elif (s[right]!='x')and(s[left]=='x'):
    left+=1
    cnt+=1
  else:
    right-=1
    cnt+=1
print(cnt)

Solution (Python):
s=input()

left=0
right=len(s)-1

ans=0
while left<right:
  c1=s[left]
  c2=s[right]
  if c1==c2:
    left+=1
    right-=1
    continue

  elif c1!='x' and c2!='x':
    print(-1)
    exit()
  elif c1=='x':
    ans+=1
    left+=1
  elif c2=='x':
    ans+=1
    right-=1

print(ans)

Solution (Python):
import sys


def main():
    s = input()
    l,r = 0,len(s)-1
    count = 0
    while l < r:
        if s[l] == s[r]:
            l += 1
            r -= 1
        else:
            if s[l] == "x":
                l += 1
            elif s[r] == "x":
                r -= 1
            else:
                print("-1")
                exit()
            count += 1
            
    print(count)
    
if __name__ == "__main__":
    main()

Solution (Python):
s = input()
l, r=0, len(s)-1
ret = 0
while l<r:
  L, R=s[l], s[r]
  if L==R:
    l+=1
    r-=1
  elif L=='x':
    l+=1
    ret+=1
  elif R=='x':
    r-=1
    ret+=1
  else:
    ret=-1
    break
print(ret)

Solution (Python):
S = list(input())

ans = 0

while len(S) > 1:
    if S[0] == S[-1]:
        S.pop(0)
        S.pop(-1)
    elif S[0] == 'x':
        ans += 1
        S.pop(0)
    elif S[-1] == 'x':
        ans += 1
        S.pop(-1)
    else:
        ans = -1
        break

print(ans)

Solution (Python):
S=input()
T=S.replace("x","")
if all(s==t for s,t in zip(T,T[::-1])):
    ans=0
    i=0
    j=len(S)-1
    while i<j:
        pi=i
        pj=j
        while i<len(S) and S[i]=="x":
            i+=1
        while j>=0 and S[j]=="x":
            j-=1
        if i>j:
            break
        ans+=abs(i-pi-pj+j)
        i+=1
        j-=1
    print(ans)
else:
    print(-1)

Solution (Python):
s = input()
l,r = 0, len(s)-1
res = 0
if s==s[::-1]:
    print(res)
    exit()

while l<r:
    if s[l]==s[r]:
        l += 1
        r -= 1
    else:
        if l==r:
            print(res)
            break
        elif s[l]!='x' and s[r]!='x':
            print(-1)
            break
        elif s[l]=='x':
            l += 1
            res += 1
        elif s[r]=='x':
            r -= 1
            res += 1
else:
    print(res)

Solution (Python):
s = input()
count = 0
if len(set(s)) == 1:
    print(0)
    exit()
left = 0
right = len(s) - 1

ans = 0
while True:
    if (s[left] == 'x' and s[right] == 'x') or s[left] == s[right]:
        left += 1
        right -= 1
    elif s[left] == 'x' and s[right] != 'x':
        ans += 1
        left += 1
    elif s[left] != 'x' and s[right] == 'x':
        ans += 1
        right -= 1
    elif s[left] != 'x' and s[right] != 'x' and s[left] != s[right]:
        print(-1)
        exit()
    if right <= left:
        break
print(ans)

Solution (Python):
s = input()
no_x = s.replace('x','')

l = 0
r = len(s)-1
ans = 0
while l<r :
  if s[l] != s[r]:
    ans += 1
    if s[l] == 'x':
      l += 1
    else:
      r -= 1  
  else:
    l += 1
    r -= 1  

for i in range(len(no_x)//2):
  if no_x[i] != no_x[-i-1]:
    ans = -1

print(ans)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  i_f=0
  i_b=len(s)-1

  ans=0
  while True:
    if i_f>=i_b:
      return ans

    
    if s[i_f]==s[i_b]:
      i_f+=1
      i_b-=1
    else:
      if s[i_f]=='x':
        i_f+=1
        ans+=1
      elif s[i_b]=='x':
        i_b-=1
        ans+=1
      else:
        return -1


print(main())

Solution (Python):
s = input()
l, r = 0, len(s) - 1
ans = 0
while l < r:
    
    if s[l] == s[r]:
        l += 1
        r -= 1
    else:
        if s[l] == "x":
            l += 1
        elif s[r] == "x":
            r -= 1
        else:
            print(-1)
            exit()
        ans += 1
print(ans)

Solution (Python):
def main() -> None:
    S = input()
    left, right = 0, len(S) - 1
    ans = 0
    while left < right:
        if S[left] == S[right]:
            left, right = left + 1, right - 1
        elif S[left] == 'x':
            ans += 1
            left += 1
        elif S[right] == 'x':
            ans += 1
            right -= 1
        else:
            print(-1)
            return
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
n = len(s)
t = s.replace("x","")
if not t:
  print(0)
  exit()
l = len(t)
if t != t[::-1]:
  print(-1)
  exit()
plc = [-1]
for i in range(n):
  if s[i] != "x":
    plc.append(i)
plc.append(n)
l = len(plc)
if l%2 == 0:
  fm = plc[:l//2]
  lt = plc[l//2:]
else:
  fm = plc[:l//2+1]
  lt = plc[l//2:]
lt = lt[::-1]
ans = 0
for i in range(1,len(fm)):
  ans += abs((fm[i]-fm[i-1])-(lt[i-1]-lt[i]))
print(ans)

Solution (Python):
def main() -> None:
    S = input()
    N = len(S)
    pos = []
    ss = []
    for i, s in enumerate(S):
        if s != 'x':
            ss.append(s)
            pos.append(i)
    if ss != ss[::-1]:
        print(-1)
        return

    L = len(ss)
    if len(ss) == 0:
        print(0)
        return
    if L % 2 == 0:
        left, right = (L-1) // 2, L // 2
    else:
        left = right = L // 2
    left, right = left - 1, right + 1
    ans = 0
    while left >= 0 and right < L:
        ans += abs((pos[left + 1] - pos[left] - 1) -
                   (pos[right] - pos[right-1] - 1))
        left, right = left - 1, right + 1
    ans += abs((pos[0] - 0) - (N - 1 - pos[-1]))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
t = s.replace('x', '')
if all(t[i] == t[len(t) - i - 1] for i in range(len(t) // 2)):
    l = 0
    r = len(s) - 1
    count = 0
    while r - l > 0:
        if s[l] != s[r]:
            count += 1
            if s[l] == 'x':
                l += 1
            if s[r] == 'x':
                r -= 1
        else:
            l += 1
            r -= 1
    print(count)
else:
    print(-1)

Solution (Python):
s = input()
def isp(str): return 1 if str == str[::-1] else 0

x_cnt=0
nox_s=[]
for c in s:
    if c=="x":
        x_cnt+=1
    else:
        nox_s.append(c)

if not isp(nox_s):
    print("-1")
    exit()

x_lis=[]
cnt=0
for c in s:
    if c=="x":
        cnt+=1
    else:
        x_lis.append(cnt)
        cnt=0
x_lis.append(cnt)
ans=0

for i in range(len(x_lis)):
    if x_lis[i] != x_lis[-i-1]:
        ans+= abs(x_lis[i] - x_lis[-i-1])

print(int(ans/2))

Solution (Python):
s=input()
s_exit_x=''
ans = 0
for n,i in enumerate(s):
    if i !='x':
        s_exit_x += i
if s_exit_x != s_exit_x[::-1]:
    print(-1)
else:
    left=0
    right=len(s)-1
    while left < right:
        if s[left] == s[right]:
            left += 1
            right -=1
        elif s[left]=="x":
            left += 1
            ans += 1
        else:
            right -=1
            ans += 1
    print(ans)

Solution (Python):
S = input()
l = 0
r = len(S) - 1
ans = 0
while l < r:
  if S[l] == S[r]:
    l += 1
    r -= 1
  elif S[l] == "x":
    l += 1
    ans += 1
  elif S[r] == "x":
    r -= 1
    ans += 1
  else:
    print(-1)
    break
else:
  print(ans)

Solution (Python):
S = input()

d = S

left = 0
right = len(d) - 1

count = 0
while left < right:
    
    if d[right] == d[left]:
        left += 1
        right -= 1
    else:
        if d[right] == 'x':
            count += 1
            right -= 1
        elif d[left] == 'x':
            count += 1
            left += 1
        else:
            count = -1
            print(-1)
            break

if count != -1:
    print(count)

Solution (Python):
from collections import deque

S = input()

d = deque(S)


left = 0
right = len(d) - 1

count = 0
while left < right:
    
    if d[right] == d[left]:
        left += 1
        right -= 1
    else:
        if d[right] == 'x':
            count += 1
            right -= 1
        elif d[left] == 'x':
            count += 1
            left += 1
        else:
            count = -1
            print(-1)
            break

if count != -1:
    print(count)

Solution (Python):
S = input()

N = len(S)
tcnt =[0] * 128
for c in S:
    tcnt[ord(c)] += 1 if c != 'x' else 0
odd = 0
for i in range(128):
    if tcnt[i] % 2 == 1:
        if odd != 0:
            print(-1)
            exit()
        else:
            odd = i

cl = -1
cr = -1
cnt = [0] * 128
for i in range(N):
    cnt[ord(S[i])] += 1 if S[i] != 'x' else 0
    if cnt[ord(S[i])] > (tcnt[ord(S[i])] + 1) // 2:
        cr = i
        break
    elif S[i] != 'x':
        cl = i
if cl == -1:
    print(0)
    exit()

if odd != 0:
    cr = cl

ans = 0
while cl >= 0 and cr < N:
    l = cl - 1
    while l >= 0 and S[l] == 'x':
        l -= 1
    lx = cl - l - 1
    r = cr + 1
    while r < N and S[r] == 'x':
        r += 1
    rx = r - cr - 1
    ans += abs(lx - rx)

    a = ord(S[l]) if l >= 0 else 0
    b = ord(S[r]) if r < N else 0
    if a != b:
        print(-1)
        exit()
    else:
        cl = l
        cr = r

print(ans)

Solution (Python):
s = input()
flag = True
top = 0
tale = len(s)-1
ans = 0
while top < tale:
  if s[top] == 'x' and s[tale] == 'x':
    top += 1
    tale -= 1
  elif s[top] == 'x' and s[tale] != 'x':
    top += 1
  elif s[top] != 'x' and s[tale] == 'x':
    tale -= 1
  else:
    if s[top] != s[tale]:
      flag = False
      break
    else:
      top += 1
      tale -= 1
if not flag:
  print(-1)
else:
  top = 0
  tale = len(s)-1
  while top < tale:
    if s[top] == s[tale]:
      top += 1
      tale -= 1
    elif s[top] == 'x' and s[tale] != 'x':
      ans += 1
      top += 1
    elif s[top] != 'x' and s[tale] == 'x':
      ans += 1
      tale -= 1
  print(ans)

Solution (Python):
s = str(input())
n = len(s)
ans = 0
j = n-1
for i in range(n):
    if i>=j:
        break

    if s[i]=='x' and s[j]=='x':
        j-=1
        continue
    elif s[i]=='x' and s[j]!='x':
        ans +=1
        continue
    while i<j:
        if s[j]=='x':
            j-=1
            ans +=1
        elif s[i]==s[j]:
            j-=1
            break
        else:
            print(-1)
            exit()


print(ans)

Solution (Python):
S = input()
N = len(S)
L = list(S)
left_idx = 0
right_idx = N - 1
ret = 0
while left_idx < right_idx:
    l = S[left_idx]
    r = S[right_idx]
    if l == r:
        left_idx += 1
        right_idx -= 1
    elif l == "x":
        ret += 1
        left_idx += 1
    elif r == "x":
        ret += 1
        right_idx -= 1
    else:
        ret = -1
        break

print(ret)

Solution (Python):
from sys import stdin

s = stdin.readline().rstrip()

r = len(s)-1
l = 0
count = 0
while r-l > 0:
    if s[l] == s[r]:
        r -= 1
        l += 1
    elif s[l] == "x" and s[r] != "x":
        count += 1
        l += 1
    elif s[r] == "x" and s[l] != "x":
        count += 1
        r -= 1
    else:
        print(-1)
        exit()
print(count)

Solution (Python):
from sys import stdin

s = stdin.readline().rstrip()
k = ""
for i in range(len(s)):
    if s[i] != "x":
        k += s[i]

if k != k[::-1]:
    print(-1)
    exit()

man = (len(k)+1)//2
point = 0
for i,j in enumerate(s):
    if j != "x":
        point += 1
    if point == man:
        kotae = i
        break

a = s[:kotae+1]
b = s[kotae:][::-1]

A = len(a)
B = len(b)

point_a = 0
point_b = 0

total = 0

while point_a < A and point_b < B:
    if a[point_a] == b[point_b]:
        point_a += 1
        point_b += 1
    elif a[point_a] == "x" and b[point_b] != "x":
        point_a += 1
        total += 1
    else:
        point_b += 1
        total += 1

print(total)

Solution (Python):
s = input()
se = s.replace('x','')
if se != se[::-1]:
    print(-1)
else:
    xc = []
    now = 0
    for i in s:
        if i == 'x':
            now += 1
        else:
            xc.append(now)
            now = 0
    xc.append(now)
    ans = 0
    for i in range(len(xc)//2):
        ans += abs(xc[i]-xc[-i-1])
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

S = input()[:-1]
N = len(S)
ans = 0
left = 0
right = N-1

while left <= right:
    if S[left] == S[right]:
        left += 1
        right -= 1
    elif S[left] == "x":
        left += 1
        ans += 1
    elif S[right] == "x":
        right -= 1
        ans += 1
    else:
        print(-1)
        break
else:
    print(ans)

Solution (Python):
s = input()
ans = 0
left = 0
right = len(s) - 1
while left <= right:
    if s[left] != s[right]:
        if s[left] == "x":
            right += 1
        elif s[right] == "x":
            left -= 1
        else:
            ans = -1
            break
        ans += 1
    left += 1
    right -= 1

print(ans)

Solution (Python):
s = input()
ns = s.replace("x","")
l = len(ns)

if ns[:l//2] == ns[l-1:(l+1)//2-1:-1]:
    ans = 0
    left = 0
    right = len(s) - 1
    while left <= right:
        if s[left] != s[right]:
            if s[left] == "x":
                right += 1
            else:
                left -= 1
            ans += 1
        left += 1
        right -= 1
else:
    ans = -1

print(ans)

Solution (Python):
import sys

s = list(map(str, input()))

ans = 0
while s:
	if s[0] == s[-1]:
		s.pop(0)
		try:
			s.pop()
		except IndexError:
			break
	elif s[0] == 'x':
		ans += 1
		s.pop(0)
	elif s[-1] == 'x':
		ans += 1
		s.pop()
	else:
		print(-1)
		sys.exit()

print(ans)

Solution (Python):
import sys

s = input()
s_x = s.replace('x', '')

if len(s) == 1:
	print(0)
	sys.exit()

middle, mod = divmod(len(s_x),2)
middle += mod
if s_x[:middle] != s_x[::-1][:middle]:
	print(-1)
	sys.exit()

x_count = []
cnt = 0
for i in range(len(s)):
	if s[i] != 'x':
		x_count.append(cnt)
		cnt = 0
	else:
		cnt += 1
x_count.append(cnt)

ans = 0
for i in range(len(x_count)):
	ans += abs(x_count[i] - x_count[-(1+i)])

print(ans//2)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
s = readline().rstrip().decode('utf-8')

t = ""
for i in s:
    if not i == "x":
        t += i

if t[::] != t[::-1]:
    print(-1)

else:
    ans = 0
    left,right = 0,len(s)-1
    while True:
        if right - left <= 0:
            break
        if s[left] == "x" and s[right] != "x":
            ans += 1
            left += 1
        elif s[left] != "x" and s[right] == "x":
            ans += 1
            right -= 1
        else:
            left += 1
            right -= 1
    print(ans)

Solution (Python):
import sys
s=input()
ans=0
i=0
j=len(s)-1
xi=0
yj=0
while(i<j):

    while(i<len(s)-1 and s[i]=='x'):
        i+=1
        xi+=1


    while(j>0 and s[j]=='x'):
        j-=1
        yj+=1



    if(s[i]!=s[j]):
        print(-1)
        sys.exit()
    else:
        ans+=abs(xi-yj)
        xi=0
        yj=0
        i+=1
        j-=1

print(ans)

Solution (Python):
s = input()
l = 0
r = len(s)-1
ans = 0
while r-l > 0:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        ans += 1
        l += 1
    elif s[r] == 'x':
        ans += 1
        r -= 1
    else:
        ans = -1
        break
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode("utf-8").rstrip()
l = 0
r = len(s) - 1

cnt = 0
while len(s) != 0:
    if s[0] == s[-1]:
        s = s[1:-1]
    elif s[0] == 'x':
        s += 'x'
        cnt += 1
    elif s[-1] == 'x':
        s = 'x' + s
        cnt += 1
    else:
        print(-1)
        sys.exit()

print(cnt)