input: from typing import List
  
  def min_operations_to_write_y(grid: List[List[int]]) -> int:
      """
      You are given a 0-indexed n x n grid where n is odd, and grid[r][c] is 0, 1, or 2.
  
      We say that a cell belongs to the Letter Y if it belongs to one of the following:
  
      The diagonal starting at the top-left cell and ending at the center cell of the grid.
      The diagonal starting at the top-right cell and ending at the center cell of the grid.
      The vertical line starting at the center cell and ending at the bottom border of the grid.
      The Letter Y is written on the grid if and only if:
  
      All values at cells belonging to the Y are equal.
      All values at cells not belonging to the Y are equal.
      The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
      Return the minimum number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to 0, 1, or 2.
  
      Examples:
      >>> grid = [[1,2,2],[1,1,0],[0,1,0]]
      3
      >>> grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
      12
  
      Constraints:
      3 <= n <= 49
      n == grid.length == grid[i].length
      0 <= grid[i][j] <= 2
      n is odd.
      """
  
output: 
      n = len(grid)
      center = n // 2
      
      # Collect values for the Y and non-Y cells
      y_values = set()
      non_y_values = set()
      
      for i in range(n):
          for j in range(n):
              if i == center or j == center or (i == j) or (i + j == n - 1):
                  y_values.add(grid[i][j])
              else:
                  non_y_values.add(grid[i][j])
      
      # If Y and non-Y values are already distinct and consistent, no operations needed
      if len(y_values) == 1 and len(non_y_values) == 1 and y_values != non_y_values:
          return 0
      
      # Calculate the minimum operations needed
      min_operations = float('inf')
      
      for y_val in range(3):
          for non_y_val in range(3):
              if y_val != non_y_val:
                  operations = 0
                  for i in range(n):
                      for j in range(n):
                          if i == center or j == center or (i == j) or (i + j == n - 1):
                              if grid[i][j] != y_val:
                                  operations += 1
                          else:
                              if grid[i][j] != non_y_val:
                                  operations += 1
                  min_operations = min(min_operations, operations)
      
      return min_operations
  