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

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

d = A("d")
l = B("l")
m = B("m")
c = A("c")
k = A("k")

d.x = m
d.r = c
l.s = m
m.s = l
c.x = m
c.r = d
k.x = l
k.r = d
assert m.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.r: A = None
        self.txt = txt

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

d = A("d")
l = B("l")
m = B("m")
c = A("c")
k = A("k")

d.x = m
d.r = c
l.s = m
m.s = l
c.x = m
c.r = d
k.x = l
k.r = d
assert m.s.s.s.txt == "l"