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

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

j = A("j")
i = B("i")
b = B("b")
d = A("d")

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

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

j = A("j")
i = B("i")
b = B("b")
d = A("d")

j.p = d
j.u = b
i.s = b
b.s = i
d.p = j
d.u = b
assert b.s.s.s.s.txt == "b"