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

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

g = A("g")
b = B("b")
c = A("c")

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

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

g = A("g")
b = B("b")
c = A("c")

g.r = b
b.r = g
c.r = b
assert g.r.r.r.txt == "b"