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

class B:
    def __init__(self, txt: str):
        self.y: A = None
        self.z: A = None
        self.txt = txt

h = A("h")
k = B("k")
b = A("b")
e = B("e")
d = B("d")

h.y = b
k.y = h
k.z = h
b.y = h
e.y = h
e.z = b
d.y = h
d.z = h
assert h.y.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.y: A = None
        self.z: A = None
        self.txt = txt

h = A("h")
k = B("k")
b = A("b")
e = B("e")
d = B("d")

h.y = b
k.y = h
k.z = h
b.y = h
e.y = h
e.z = b
d.y = h
d.z = h
assert h.y.y.y.y.txt == "h"