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

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

n = A("n")
d = B("d")
b = B("b")
g = A("g")

n.z = g
n.p = g
d.y = n
b.y = g
g.z = n
g.p = n
assert d.y.z.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.p: A = None
        self.txt = txt

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

n = A("n")
d = B("d")
b = B("b")
g = A("g")

n.z = g
n.p = g
d.y = n
b.y = g
g.z = n
g.p = n
assert d.y.z.p.p.txt == "g"