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

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

g = A("g")
l = B("l")
a = B("a")
b = B("b")
h = A("h")
d = A("d")
m = B("m")
i = B("i")

g.z = d
l.p = a
a.p = m
b.p = a
h.z = g
d.z = h
m.p = a
i.p = a
assert g.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

g = A("g")
l = B("l")
a = B("a")
b = B("b")
h = A("h")
d = A("d")
m = B("m")
i = B("i")

g.z = d
l.p = a
a.p = m
b.p = a
h.z = g
d.z = h
m.p = a
i.p = a
assert g.z.z.z.z.txt == "d"