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

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

n = A("n")
d = B("d")
k = A("k")
m = B("m")
c = B("c")

n.p = d
n.w = m
d.z = n
d.p = n
k.p = c
k.w = m
m.z = k
m.p = n
c.z = n
c.p = k
assert n.p.z.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.w: B = None
        self.txt = txt

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

n = A("n")
d = B("d")
k = A("k")
m = B("m")
c = B("c")

n.p = d
n.w = m
d.z = n
d.p = n
k.p = c
k.w = m
m.z = k
m.p = n
c.z = n
c.p = k
assert n.p.z.p.txt == "d"