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

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

g = A("g")
e = B("e")
i = A("i")
m = B("m")
a = A("a")
d = A("d")

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

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

g = A("g")
e = B("e")
i = A("i")
m = B("m")
a = A("a")
d = A("d")

g.z = e
e.p = g
i.z = e
m.p = d
a.z = e
d.z = m
assert m.p.z.p.z.p.z.txt == "m"