from collections import Counter def remove_sequence(hand: list[int], start: int) -> list[int]: hand_picked = hand.copy() hand_picked.remove(start) hand_picked.remove(start+1) hand_picked.remove(start+2) return hand_picked def remove_head(hand: list[int], head_num: int) -> list[int]: hand_picked = hand.copy() for _ in range(2): hand_picked.remove(head_num) return hand_picked def remove_anko(hand: list[int], anko_num: int) -> list[int]: hand_picked = hand.copy() for _ in range(3): hand_picked.remove(anko_num) return hand_picked def is_agari(hand: list[int], head: int = 1, ments: int = 4) -> bool: hand.sort() assert head >= 0 assert ments >= 0 assert len(hand) == head * 2 + ments * 3 hand_count = Counter(hand) if head == 1 and ments == 0: return len(hand_count) == 1 if head == 0 and ments == 1: if hand[2]-hand[1] == 1 and hand[1]-hand[0] == 1: return True if len(hand_count) == 1: return True return False if len(hand) == 14: if len(hand_count) == 7 and len(set(hand_count.values())) == 1: return True hand.sort() for num in range(1, 10): match hand_count[num]: case 0: continue case 1: if ments == 0: continue try: hand_picked = remove_sequence(hand, num) except ValueError: continue if is_agari(hand_picked, head=head, ments=ments-1): return True case 2: if ments > 0: try: hand_picked = remove_sequence(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head, ments=ments-1): return True if head > 0: try: hand_picked = remove_head(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head-1, ments=ments): return True case 3: if ments > 0: try: hand_picked = remove_sequence(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head, ments=ments-1): return True try: hand_picked = remove_anko(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head, ments=ments-1): return True if head > 0: try: hand_picked = remove_head(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head-1, ments=ments): return True case 4: if ments > 0: try: hand_picked = remove_sequence(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head, ments=ments-1): return True try: hand_picked = remove_anko(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head, ments=ments-1): return True if head > 0: try: hand_picked = remove_head(hand, num) except ValueError: pass else: if is_agari(hand_picked, head=head-1, ments=ments): return True return False def main(): hand = list(map(int, list(input()))) for num in range(1, 10): if hand.count(num) == 4: continue tumo = hand.copy() tumo.append(num) if is_agari(tumo): print(num) if __name__ == "__main__": main()