class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.s: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: B = None
        self.s: A = None
        self.txt = txt

n = A("n")
e = B("e")
b = B("b")
g = A("g")

n.r = e
n.s = b
e.p = b
e.s = n
b.p = e
b.s = g
g.r = b
g.s = b
assert b.s.s.p.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.s: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: B = None
        self.s: A = None
        self.txt = txt

n = A("n")
e = B("e")
b = B("b")
g = A("g")

n.r = e
n.s = b
e.p = b
e.s = n
b.p = e
b.s = g
g.r = b
g.s = b
assert b.s.s.p.txt == "e"