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

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

m = A("m")
l = B("l")
b = A("b")
c = B("c")

m.y = l
m.w = b
l.r = b
b.y = c
b.w = m
c.r = m
assert c.r.w.y.r.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.w: A = None
        self.txt = txt

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

m = A("m")
l = B("l")
b = A("b")
c = B("c")

m.y = l
m.w = b
l.r = b
b.y = c
b.w = m
c.r = m
assert c.r.w.y.r.txt == "m"