# Path: siebenapp/domain.py
# class Graph:
#     """Base interface definition"""
# 
#     NO_VALUE = "no value"
# 
#     def __init__(self, goaltree=None):
#         self.goaltree: Graph = goaltree
# 
#     def __getattr__(self, item):
#         """When method is not found, ask nested goaltree for it"""
#         if self.goaltree != self:
#             return getattr(self.goaltree, item)
#         return None
# 
#     def accept(self, command: Command) -> None:
#         """React on the given command"""
#         method_name = "accept_" + command.__class__.__name__
#         if method := getattr(self, method_name):
#             method(command)
#         elif parent := self.goaltree:
#             parent.accept(command)
#         else:
#             raise NotImplementedError(f"Cannot find method {method_name}")
# 
#     def accept_all(self, *commands: Command) -> None:
#         """React on the command chain"""
#         for command in commands:
#             self.accept(command)
# 
#     def settings(self, key: str) -> Any:
#         """Returns given inner value by the key"""
#         if self.goaltree is not None:
#             return self.goaltree.settings(key)
#         return Graph.NO_VALUE
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         """Synchronize *non-persistent* settings from the given origin"""
#         if self.goaltree:
#             self.goaltree.reconfigure_from(origin)
# 
#     def selections(self) -> Set[int]:
#         """Return ids of special 'selection' goals"""
#         if self.goaltree:
#             return self.goaltree.selections()
#         raise NotImplementedError
# 
#     def events(self) -> deque:
#         """Returns queue of applied actions.
#         Note: this queue is modifiable -- you may push new events into it. But this
#         behavior may be changed in future."""
#         if self.goaltree is not None:
#             return self.goaltree.events()
#         raise NotImplementedError
# 
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         """Run search query against content"""
#         raise NotImplementedError
# 
#     def error(self, message: str) -> None:
#         """Show error message"""
#         if self.goaltree is not None:
#             self.goaltree.error(message)
# 
#     def verify(self) -> bool:
#         """Check all inner data for correctness"""
#         if self.goaltree is not None:
#             return self.goaltree.verify()
#         return False
# 
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int


import math
from typing import List, Dict, Tuple, Any, Set, Iterable
from siebenapp.domain import Graph, Select

class BidirectionalIndex:
    NOT_FOUND = -2

    def __init__(self, goals: Iterable[int]):
        self.m = {g: i + 1 for i, g in enumerate(sorted(g for g in goals if g > 0))}
        self.length = len(self.m)

    def forward(self, goal_id: int) -> int:
        if goal_id < 0:
            return goal_id
        goal_id = self.m[goal_id]
        new_id = goal_id % 10
        if self.length > 10:
            new_id += 10 * ((goal_id - 1) // 10 + 1)
        if self.length > 90:
            new_id += 100 * ((goal_id - 1) // 100 + 1)
        if self.length > 900:
            new_id += 1000 * ((goal_id - 1) // 1000 + 1)
        return new_id

    def backward(self, goal_id: int) -> int:
        possible_selections: List[int] = [
            g for g in self.m if self.forward(g) == goal_id
        ]
        if len(possible_selections) == 1:
            return possible_selections[0]
        return BidirectionalIndex.NOT_FOUND



# Path: siebenapp/domain.py
# class Graph:
#     """Base interface definition"""
# 
#     NO_VALUE = "no value"
# 
#     def __init__(self, goaltree=None):
#         self.goaltree: Graph = goaltree
# 
#     def __getattr__(self, item):
#         """When method is not found, ask nested goaltree for it"""
#         if self.goaltree != self:
#             return getattr(self.goaltree, item)
#         return None
# 
#     def accept(self, command: Command) -> None:
#         """React on the given command"""
#         method_name = "accept_" + command.__class__.__name__
#         if method := getattr(self, method_name):
#             method(command)
#         elif parent := self.goaltree:
#             parent.accept(command)
#         else:
#             raise NotImplementedError(f"Cannot find method {method_name}")
# 
#     def accept_all(self, *commands: Command) -> None:
#         """React on the command chain"""
#         for command in commands:
#             self.accept(command)
# 
#     def settings(self, key: str) -> Any:
#         """Returns given inner value by the key"""
#         if self.goaltree is not None:
#             return self.goaltree.settings(key)
#         return Graph.NO_VALUE
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         """Synchronize *non-persistent* settings from the given origin"""
#         if self.goaltree:
#             self.goaltree.reconfigure_from(origin)
# 
#     def selections(self) -> Set[int]:
#         """Return ids of special 'selection' goals"""
#         if self.goaltree:
#             return self.goaltree.selections()
#         raise NotImplementedError
# 
#     def events(self) -> deque:
#         """Returns queue of applied actions.
#         Note: this queue is modifiable -- you may push new events into it. But this
#         behavior may be changed in future."""
#         if self.goaltree is not None:
#             return self.goaltree.events()
#         raise NotImplementedError
# 
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         """Run search query against content"""
#         raise NotImplementedError
# 
#     def error(self, message: str) -> None:
#         """Show error message"""
#         if self.goaltree is not None:
#             self.goaltree.error(message)
# 
#     def verify(self) -> bool:
#         """Check all inner data for correctness"""
#         if self.goaltree is not None:
#             return self.goaltree.verify()
#         return False
# 
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int


# Path: siebenapp/enumeration.py
import math
from typing import List, Dict, Tuple, Any, Set, Iterable
from siebenapp.domain import Graph, Select


class BidirectionalIndex:
    NOT_FOUND = -2

    def __init__(self, goals: Iterable[int]):
        self.m = {g: i + 1 for i, g in enumerate(sorted(g for g in goals if g > 0))}
        self.length = len(self.m)

    def forward(self, goal_id: int) -> int:
        if goal_id < 0:
            return goal_id
        goal_id = self.m[goal_id]
        new_id = goal_id % 10
        if self.length > 10:
            new_id += 10 * ((goal_id - 1) // 10 + 1)
        if self.length > 90:
            new_id += 100 * ((goal_id - 1) // 100 + 1)
        if self.length > 900:
            new_id += 1000 * ((goal_id - 1) // 1000 + 1)
        return new_id

    def backward(self, goal_id: int) -> int:
        possible_selections: List[int] = [
            g for g in self.m if self.forward(g) == goal_id
        ]
        if len(possible_selections) == 1:
            return possible_selections[0]
        return BidirectionalIndex.NOT_FOUND



class Enumeration(Graph):