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

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

n = A("n")
a = B("a")
d = A("d")
b = B("b")
h = A("h")
e = A("e")
g = B("g")
c = B("c")

n.p = b
a.z = g
a.x = n
d.p = b
b.z = a
b.x = n
h.p = g
e.p = a
g.z = a
g.x = h
c.z = b
c.x = d
assert g.x.p.z.x.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

n = A("n")
a = B("a")
d = A("d")
b = B("b")
h = A("h")
e = A("e")
g = B("g")
c = B("c")

n.p = b
a.z = g
a.x = n
d.p = b
b.z = a
b.x = n
h.p = g
e.p = a
g.z = a
g.x = h
c.z = b
c.x = d
assert g.x.p.z.x.p.z.txt == "a"