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

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

g = A("g")
e = B("e")
h = B("h")
d = A("d")

g.z = d
g.x = h
e.r = h
h.r = e
d.z = g
d.x = h
assert h.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.x: B = None
        self.txt = txt

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

g = A("g")
e = B("e")
h = B("h")
d = A("d")

g.z = d
g.x = h
e.r = h
h.r = e
d.z = g
d.x = h
assert h.r.r.r.txt == "e"