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

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

d = A("d")
f = B("f")
h = B("h")
b = A("b")

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

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

d = A("d")
f = B("f")
h = B("h")
b = A("b")

d.p = f
f.p = d
h.p = b
b.p = f
assert d.p.p.p.txt == "f"