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

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

m = A("m")
k = B("k")
j = B("j")
d = B("d")

m.y = d
k.w = m
j.w = m
d.w = m
assert m.y.w.y.w.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

m = A("m")
k = B("k")
j = B("j")
d = B("d")

m.y = d
k.w = m
j.w = m
d.w = m
assert m.y.w.y.w.txt == "m"