output_description: If we cannot turn S into a palindrome, print `-1`. Otherwise, print the
  minimum required number of operations.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      S
attempt: def count_inversions(L):
      def helper(A, B):
          lA, lB = len(A), len(B)
          if lA == 0:
              return B, 0
          elif lB == 0:
              return A, 0
  
          A, c1 = helper(A[: lA // 2], A[lA // 2 :])
          B, c2 = helper(B[: lB // 2], B[lB // 2 :])
          cnt = c1 + c2
          i = 0
          j = 0
  
          C = [None] * (lA + lB)
          while i < lA and j < lB:
              if A[i] > B[j]:
                  cnt += lA - i
                  C[i + j] = B[j]
                  j += 1
              else:
                  C[i + j] = A[i]
                  i += 1
  
          if i < lA:
              C[i + j :] = A[i:]
          else:
              C[i + j :] = B[j:]
  
          return C, cnt
  
      return helper(L[: len(L) // 2], L[len(L) // 2 :])[1]
  
  
  from collections import deque
  
  ALPHABETS = 26
  
  indices = [deque() for _ in range(26)]
  S = list(map(lambda c: ord(c) - ord("a"), input()))
  for i, c in enumerate(S):
      indices[c] += [i]
  
  odd = None
  for c, l in enumerate(indices):
      if len(l) % 2 == 1:
          if odd is None:
              odd = c
          else:
              odd = -1
              break
  
  if odd == -1:
      print(-1)
      exit()
  
  target = [None] * len(S)
  assigned = [False] * len(S)
  if odd is not None:
      l = list(indices[odd])
      i = l[len(l) // 2]
      target[len(target) // 2] = i
      assigned[i] = True
      l = deque(l[: len(l) // 2] + l[len(l) // 2 + 1 :])
  
  j = 0
  for i in range(len(S) // 2):
      while assigned[j]:
          j += 1
  
      l = indices[S[j]]
      a = l.popleft()
      b = l.pop()
      assert a == j
      target[i] = a
      target[len(S) - i - 1] = b
      assigned[a] = True
      assigned[b] = True
  
  print(count_inversions(target))
  
problem_description: Statement
  
  You are given a string S consisting of lowercase English letters. Determine
  whether we can turn S into a palindrome by repeating the operation of swapping
  two adjacent characters. If it is possible, find the minimum required number
  of operations.
samples: [{"input": "eel", "output": "1\n    \n\nWe can turn S into a palindrome by the following operation:\n\n  * Swap the 2-nd and 3-rd characters. S is now `ele`.\n\n* * *"}, {"input": "ataatmma", "output": "4\n    \n\nWe can turn S into a palindrome by the following operation:\n\n  * Swap the 5-th and 6-th characters. S is now `ataamtma`.\n  * Swap the 4-th and 5-th characters. S is now `atamatma`.\n  * Swap the 3-rd and 4-th characters. S is now `atmaatma`.\n  * Swap the 2-nd and 3-rd characters. S is now `amtaatma`.\n\n* * *"}, {"input": "snuke", "output": "-1\n    \n\nWe cannot turn S into a palindrome."}]