

'''simple docstring'''



from abc import ABC, abstractmethod
from typing import List, Optional







class 		lowerCAmelCase__  ( __lowercase   ):



 """simple docstring"""



 def __init__(						self   :			Union[str, Any]     )    ->					Tuple:


      """simple docstring"""




      self.test()
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = 0
      __SCREAMING_SNAKE_CASE      = False
      while not completed:
           if counter == 1:
                self.reset()
           __SCREAMING_SNAKE_CASE      = self.advance()
           if not self.does_advance(_A     ):
                raise Exception(
                    """Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."""     )

           __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = self.update(_A     )
           counter += 1

           if counter > 10_000:
                raise Exception("""update() does not fulfill the constraint."""     )

      if self.remaining() != 0:
           raise Exception("""Custom Constraint is not defined correctly."""     )
 @abstractmethod
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					List[str]:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )
 @abstractmethod
 def  UpperCAmelCase__						(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					List[Any]:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )
 @abstractmethod
 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Dict:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )
 @abstractmethod
 def  UpperCAmelCase__						(						self   :			Optional[Any]     )    ->					Any:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )
 @abstractmethod
 def  UpperCAmelCase__						(						self   :			str     )    ->					Optional[Any]:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )


 @abstractmethod
 def  UpperCAmelCase__						(						self   :			Optional[int]   ,				__SCREAMING_SNAKE_CASE   :			str=False     )    ->					str:


      """simple docstring"""




      raise NotImplementedError(
          f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'     )







class 		lowerCAmelCase__  ( __lowercase   ):



 """simple docstring"""



 def __init__(						self   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			List[int]     )    ->					int:


      """simple docstring"""




      super(_A   ,				self     ).__init__()

      if not isinstance(_A   ,				_A     ) or len(_A     ) == 0:
           raise ValueError(f'`token_ids` has to be a non-empty list, but is {token_ids}.'     )
      if any((not isinstance(_A   ,				_A     ) or token_id < 0) for token_id in token_ids     ):
           raise ValueError(f'Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'     )

      __SCREAMING_SNAKE_CASE      = token_ids

      __SCREAMING_SNAKE_CASE      = len(self.token_ids     )
      __SCREAMING_SNAKE_CASE      = -1  # the index of the currently fulfilled step
      __SCREAMING_SNAKE_CASE      = False
 def  UpperCAmelCase__						(						self   :			Optional[int]     )    ->					List[Any]:


      """simple docstring"""




      if self.completed:
           return None
      return self.token_ids[self.fulfilled_idx + 1]
 def  UpperCAmelCase__						(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Optional[Any]:


      """simple docstring"""




      if not isinstance(_A   ,				_A     ):
           raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_A     )}'     )

      if self.completed:
           return False

      return token_id == self.token_ids[self.fulfilled_idx + 1]
 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Optional[int]:


      """simple docstring"""




      if not isinstance(_A   ,				_A     ):
           raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_A     )}'     )

      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = False

      if self.does_advance(_A     ):
           self.fulfilled_idx += 1
           __SCREAMING_SNAKE_CASE      = True
           if self.fulfilled_idx == (self.seqlen - 1):
                __SCREAMING_SNAKE_CASE      = True
           __SCREAMING_SNAKE_CASE      = completed
      else:
           # failed to make progress.
           __SCREAMING_SNAKE_CASE      = True
           self.reset()
      return stepped, completed, reset
 def  UpperCAmelCase__						(						self   :			Optional[int]     )    ->					Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = 0
 def  UpperCAmelCase__						(						self   :			str     )    ->					Any:


      """simple docstring"""




      return self.seqlen - (self.fulfilled_idx + 1)


 def  UpperCAmelCase__						(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			Optional[int]=False     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = PhrasalConstraint(self.token_ids     )

      if stateful:
           __SCREAMING_SNAKE_CASE      = self.seqlen
           __SCREAMING_SNAKE_CASE      = self.fulfilled_idx
           __SCREAMING_SNAKE_CASE      = self.completed

      return new_constraint







class 		lowerCAmelCase__  :



 """simple docstring"""



 def __init__(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			List[List[int]]   ,				__SCREAMING_SNAKE_CASE   :			Optional[int]=True     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = max([len(_A     ) for one in nested_token_ids]     )

      __SCREAMING_SNAKE_CASE      = {}
      for token_ids in nested_token_ids:
           __SCREAMING_SNAKE_CASE      = root
           for tidx, token_id in enumerate(_A     ):
                if token_id not in level:
                     __SCREAMING_SNAKE_CASE      = {}

                __SCREAMING_SNAKE_CASE      = level[token_id]

      if no_subsets and self.has_subsets(_A   ,				_A     ):
           raise ValueError(
               """Each list in `nested_token_ids` can\'t be a complete subset of another list, but is"""
               f' {nested_token_ids}.'     )

      __SCREAMING_SNAKE_CASE      = root
 def  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.trie

      for current_token in current_seq:
           __SCREAMING_SNAKE_CASE      = start[current_token]

      __SCREAMING_SNAKE_CASE      = list(start.keys()     )

      return next_tokens
 def  UpperCAmelCase__						(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]     )    ->					List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.next_tokens(_A     )

      return len(_A     ) == 0
 def  UpperCAmelCase__						(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			List[str]     )    ->					Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = list(root.values()     )
      if len(_A     ) == 0:
           return 1
      else:
           return sum([self.count_leaves(_A     ) for nn in next_nodes]     )


 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Any     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.count_leaves(_A     )
      return len(_A     ) != leaf_count







class 		lowerCAmelCase__  ( __lowercase   ):



 """simple docstring"""



 def __init__(						self   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			List[List[int]]     )    ->					Optional[Any]:


      """simple docstring"""




      super(_A   ,				self     ).__init__()

      if not isinstance(_A   ,				_A     ) or len(_A     ) == 0:
           raise ValueError(f'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'     )
      if any(not isinstance(_A   ,				_A     ) for token_ids in nested_token_ids     ):
           raise ValueError(f'`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'     )
      if any(
          any((not isinstance(_A   ,				_A     ) or token_id < 0) for token_id in token_ids     )
          for token_ids in nested_token_ids     ):
           raise ValueError(
               f'Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'     )

      __SCREAMING_SNAKE_CASE      = DisjunctiveTrie(_A     )
      __SCREAMING_SNAKE_CASE      = nested_token_ids

      __SCREAMING_SNAKE_CASE      = self.trie.max_height
      __SCREAMING_SNAKE_CASE      = []
      __SCREAMING_SNAKE_CASE      = False
 def  UpperCAmelCase__						(						self   :			List[Any]     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.trie.next_tokens(self.current_seq     )

      if len(_A     ) == 0:
           return None
      else:
           return token_list
 def  UpperCAmelCase__						(						self   :			Optional[int]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Optional[int]:


      """simple docstring"""




      if not isinstance(_A   ,				_A     ):
           raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_A     )}'     )

      __SCREAMING_SNAKE_CASE      = self.trie.next_tokens(self.current_seq     )

      return token_id in next_tokens
 def  UpperCAmelCase__						(						self   :			Union[str, Any]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Any:


      """simple docstring"""




      if not isinstance(_A   ,				_A     ):
           raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_A     )}'     )

      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = False

      if self.does_advance(_A     ):
           self.current_seq.append(_A     )
           __SCREAMING_SNAKE_CASE      = True
      else:
           __SCREAMING_SNAKE_CASE      = True
           self.reset()

      __SCREAMING_SNAKE_CASE      = self.trie.reached_leaf(self.current_seq     )
      __SCREAMING_SNAKE_CASE      = completed

      return stepped, completed, reset
 def  UpperCAmelCase__						(						self   :			List[Any]     )    ->					List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = False
      __SCREAMING_SNAKE_CASE      = []
 def  UpperCAmelCase__						(						self   :			int     )    ->					Union[str, Any]:


      """simple docstring"""




      if self.completed:
           # since this can be completed without reaching max height
           return 0
      else:
           return self.seqlen - len(self.current_seq     )


 def  UpperCAmelCase__						(						self   :			List[str]   ,				__SCREAMING_SNAKE_CASE   :			int=False     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = DisjunctiveConstraint(self.token_ids     )

      if stateful:
           __SCREAMING_SNAKE_CASE      = self.seqlen
           __SCREAMING_SNAKE_CASE      = self.current_seq
           __SCREAMING_SNAKE_CASE      = self.completed

      return new_constraint







class 		lowerCAmelCase__  :



 """simple docstring"""



 def __init__(						self   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			List[Constraint]     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = constraints

      # max # of steps required to fulfill a given constraint
      __SCREAMING_SNAKE_CASE      = max([c.seqlen for c in constraints]     )
      __SCREAMING_SNAKE_CASE      = len(_A     )
      __SCREAMING_SNAKE_CASE      = False

      self.init_state()
 def  UpperCAmelCase__						(						self   :			List[str]     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = []
      __SCREAMING_SNAKE_CASE      = None
      __SCREAMING_SNAKE_CASE      = [constraint.copy(stateful=_A     ) for constraint in self.constraints]
 def  UpperCAmelCase__						(						self   :			int     )    ->					Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = 0
      if self.inprogress_constraint:
           # extra points for having a constraint mid-fulfilled
           add += self.max_seqlen - self.inprogress_constraint.remaining()

      return (len(self.complete_constraints     ) * self.max_seqlen) + add
 def  UpperCAmelCase__						(						self   :			List[Any]     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = []
      if self.inprogress_constraint is None:
           for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
                __SCREAMING_SNAKE_CASE      = constraint.advance()
                if isinstance(_A   ,				_A     ):
                     token_list.append(_A     )
                elif isinstance(_A   ,				_A     ):
                     token_list.extend(_A     )
      else:
           __SCREAMING_SNAKE_CASE      = self.inprogress_constraint.advance()
           if isinstance(_A   ,				_A     ):
                token_list.append(_A     )
           elif isinstance(_A   ,				_A     ):
                token_list.extend(_A     )

      if len(_A     ) == 0:
           return None
      else:
           return token_list
 def  UpperCAmelCase__						(						self   :			Optional[int]   ,				__SCREAMING_SNAKE_CASE   :			Optional[List[int]]     )    ->					Union[str, Any]:


      """simple docstring"""




      self.init_state()

      if token_ids is not None:
           for token in token_ids:
                # completes or steps **one** constraint
                __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = self.add(_A     )

                # the entire list of constraints are fulfilled
                if self.completed:
                     break
 def  UpperCAmelCase__						(						self   :			Union[str, Any]   ,				__SCREAMING_SNAKE_CASE   :			int     )    ->					Optional[Any]:


      """simple docstring"""




      if not isinstance(_A   ,				_A     ):
           raise ValueError(f'`token_id` should be an `int`, but is `{token_id}`.'     )

      __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = False, False

      if self.completed:
           __SCREAMING_SNAKE_CASE      = True
           __SCREAMING_SNAKE_CASE      = False
           return complete, stepped

      if self.inprogress_constraint is not None:
           # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
           # job, simply update the state

           __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = self.inprogress_constraint.update(_A     )
           if reset:
                # 1. If the next token breaks the progress, then we must restart.
                #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

                #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
                #     constraint, not the full list of constraints.

                self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_A     )     )
                __SCREAMING_SNAKE_CASE      = None

           if complete:
                # 2. If the next token completes the constraint, move it to completed list, set
                #     inprogress to None. If there are no pending constraints either, then this full list of constraints
                #     is complete.

                self.complete_constraints.append(self.inprogress_constraint     )
                __SCREAMING_SNAKE_CASE      = None

                if len(self.pending_constraints     ) == 0:
                     # we're done!
                     __SCREAMING_SNAKE_CASE      = True

      else:
           # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
           # of constraints?

           for cidx, pending_constraint in enumerate(self.pending_constraints     ):
                if pending_constraint.does_advance(_A     ):
                     __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = pending_constraint.update(_A     )

                     if not stepped:
                          raise Exception(
                              """`constraint.update(token_id)` is not yielding incremental progress, """
                              """even though `constraint.does_advance(token_id)` is true."""     )

                     if complete:
                          self.complete_constraints.append(_A     )
                          __SCREAMING_SNAKE_CASE      = None

                     if not complete and stepped:
                          __SCREAMING_SNAKE_CASE      = pending_constraint

                     if complete or stepped:
                          # If we made any progress at all, then it's at least not a "pending constraint".

                          __SCREAMING_SNAKE_CASE      = (
                              self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
                          )

                          if len(self.pending_constraints     ) == 0 and self.inprogress_constraint is None:
                               # If there's no longer any pending after this and no inprogress either, then we must be
                               # complete.

                               __SCREAMING_SNAKE_CASE      = True

                          break  # prevent accidentally stepping through multiple constraints with just one token.

      return complete, stepped


 def  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]=True     )    ->					List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = ConstraintListState(self.constraints     )  # we actually never though self.constraints objects
      # throughout this process. So it's at initialization state.

      if stateful:
           __SCREAMING_SNAKE_CASE      = [
               constraint.copy(stateful=_A     ) for constraint in self.complete_constraints
           ]
           if self.inprogress_constraint is not None:
                __SCREAMING_SNAKE_CASE      = self.inprogress_constraint.copy(stateful=_A     )
           __SCREAMING_SNAKE_CASE      = [constraint.copy() for constraint in self.pending_constraints]

      return new_state







from __future__ import annotations

_lowerCAmelCase       =		[True] * 1_000_001
_lowerCAmelCase       =		2
while i * i <= 1_000_000:
					if seive[i]:
										for j in range(i * i, 1_000_001, i):
															_lowerCAmelCase       =		False
					i += 1
def 					_snake_case	(  __snake_case						):
					return seive[n]
def 					_snake_case	(  __snake_case						):
					return any(digit in '''02468''' for digit in str(__snake_case						)						)
def 					_snake_case	(  __snake_case = 1000000						):
					_UpperCamelCase			     =     [2]  # result already includes the number 2.
					for num in range(3  ,     limit + 1  ,     2						):
										if is_prime(__snake_case						) and not contains_an_even_digit(__snake_case						):
															_UpperCamelCase			     =     str(__snake_case						)
															_UpperCamelCase			     =     [int(str_num[j:] + str_num[:j]						) for j in range(len(__snake_case						)						)]
															if all(is_prime(__snake_case						) for i in list_nums						):
																				result.append(__snake_case						)
					return result
def 					_snake_case	(  ):
					return len(find_circular_primes()						)


if __name__ == "__main__":
					print(f'{len(find_circular_primes()) = }')
