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

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

j = A("j")
f = B("f")
m = A("m")

j.s = m
f.r = m
f.s = j
m.s = j
assert j.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.txt = txt

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

j = A("j")
f = B("f")
m = A("m")

j.s = m
f.r = m
f.s = j
m.s = j
assert j.s.s.s.txt == "m"