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

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

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

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

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

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

d.p = b
d.x = m
m.p = h
h.p = m
h.x = m
b.p = d
assert b.p.x.p.p.txt == "m"