new_contents: class Solution:
      def minChanges(self, nums: List[int], m: int) -> int:
          n = len(nums)
          z = 2**10
          s = [[] for _ in range(m)]
          for i in range(n):
              s[i % m].append(nums[i])
          def keyfunc(s):
              return len(set(s))
          s.sort(key=keyfunc)
          ss = [{} for _ in range(m)]
          for i in range(m):
              for j in s[i]:
                  if not j in ss[i]:
                      ss[i][j] = 1
                  else:
                      ss[i][j] += 1
          #print(s,ss)
          g = [0 for _ in range(m+1)]
          g1 = [0 for _ in range(m+1)]
          for i in range(m-1,-1,-1):
              g[i] = g[i+1] + len(s[i])
          
          for i in range(m-1,-1,-1):
              tmp = 0
              for k, v in ss[i].items():
                  if v > tmp:
                      tmp = v
              g1[i] = g1[i+1] + len(s[i]) - tmp
              
          visited = {}
          hp = []
          #cp1 = 0
          #cp = 0
          def push(i, j, cost):
              #nonlocal cp, cp1
              #cp1 += 1
              if (i, j) in visited:
                  return
              #cp += 1
              heappush(hp, (cost + g1[i+1], cost + g[i+1], cost, -i, j))
          def pop():
              while hp:
                  _, _, cost, i, j = heappop(hp)
                  i = -i 
                  if (i, j) in visited:
                      continue
                  else:
                      visited[(i,j)] = cost
                      return i, j, cost
          
          push(0, -1, len(s[0]))
          for k, v in ss[0].items():
              push(0, k, len(s[0]) - v)
          while hp:
              i, j, cost = pop()
              if i == m - 1:
                  if j <= 0:
                      #print(len(hp), cp, cp1)
                      return cost
                  continue
              p = cost + len(s[i+1])
              for k, v in ss[i+1].items():
                  if j == -1:
                      if cost + len(s[i+1]) - v < p:
                          p = cost + len(s[i+1]) - v
                      continue
                  jj = j ^ k
                  cc = cost + len(s[i+1]) - v
                  push(i+1, jj, cc)
              push(i+1, -1, p)
          return -1
          
          
  
subject: Create lc1787.py