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

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

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

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

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

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

d.z = h
d.p = j
h.p = d
h.x = b
i.z = h
i.p = j
j.z = h
j.p = i
b.z = h
b.p = d
assert b.z.x.z.x.z.txt == "h"