class A:
    def __init__(self, txt: str):
        self.x: B = None
        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

g = A("g")
n = B("n")
f = B("f")
d = A("d")
a = B("a")
i = B("i")
j = A("j")
e = B("e")

g.x = n
g.p = n
n.z = f
n.x = j
f.z = a
f.x = g
d.x = i
d.p = n
a.z = n
a.x = d
i.z = f
i.x = j
j.x = e
j.p = i
e.z = n
e.x = d
assert d.x.z.x.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        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

g = A("g")
n = B("n")
f = B("f")
d = A("d")
a = B("a")
i = B("i")
j = A("j")
e = B("e")

g.x = n
g.p = n
n.z = f
n.x = j
f.z = a
f.x = g
d.x = i
d.p = n
a.z = n
a.x = d
i.z = f
i.x = j
j.x = e
j.p = i
e.z = n
e.x = d
assert d.x.z.x.p.z.txt == "f"