class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.txt = txt

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

e = A("e")
c = B("c")
h = A("h")
d = B("d")
g = A("g")
a = A("a")
b = A("b")

e.x = g
c.p = d
h.x = a
d.p = c
g.x = a
a.x = h
b.x = a
assert c.p.p.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.txt = txt

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

e = A("e")
c = B("c")
h = A("h")
d = B("d")
g = A("g")
a = A("a")
b = A("b")

e.x = g
c.p = d
h.x = a
d.p = c
g.x = a
a.x = h
b.x = a
assert c.p.p.p.p.p.txt == "d"