def elements_adjacent(elements, index1, index2):
    """Check if two elements are adjacent."""
    return (
        elements[0, index1] == elements[0, index2]
        or elements[0, index1] == elements[1, index2]
        or elements[0, index1] == elements[2, index2]
        or elements[1, index1] == elements[0, index2]
        or elements[1, index1] == elements[1, index2]
        or elements[1, index1] == elements[2, index2]
        or elements[2, index1] == elements[0, index2]
        or elements[2, index1] == elements[1, index2]
        or elements[2, index1] == elements[2, index2]
    )