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

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

b = A("b")
m = B("m")
d = A("d")

b.z = d
m.r = b
d.z = b
assert m.r.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

b = A("b")
m = B("m")
d = A("d")

b.z = d
m.r = b
d.z = b
assert m.r.z.z.txt == "b"