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

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

m = A("m")
a = B("a")
j = A("j")
b = B("b")

m.y = b
a.s = b
j.y = a
b.s = a
assert a.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

m = A("m")
a = B("a")
j = A("j")
b = B("b")

m.y = b
a.s = b
j.y = a
b.s = a
assert a.s.s.s.txt == "b"