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

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

a = A("a")
c = B("c")
d = A("d")
b = B("b")

a.s = c
c.p = d
d.s = b
b.p = d
assert d.s.p.s.txt == "
class A:
    def __init__(self, txt: str):
        self.s: B = None
        self.txt = txt

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

a = A("a")
c = B("c")
d = A("d")
b = B("b")

a.s = c
c.p = d
d.s = b
b.p = d
assert d.s.p.s.txt == "b"