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

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

n = A("n")
i = B("i")
b = B("b")
j = B("j")
a = B("a")
m = B("m")
h = B("h")
d = A("d")

n.p = d
n.z = m
i.r = n
i.w = n
b.r = d
b.w = n
j.r = n
j.w = d
a.r = n
a.w = n
m.r = d
m.w = n
h.r = d
h.w = n
d.p = n
d.z = a
assert a.r.z.r.z.w.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.z: B = None
        self.txt = txt

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

n = A("n")
i = B("i")
b = B("b")
j = B("j")
a = B("a")
m = B("m")
h = B("h")
d = A("d")

n.p = d
n.z = m
i.r = n
i.w = n
b.r = d
b.w = n
j.r = n
j.w = d
a.r = n
a.w = n
m.r = d
m.w = n
h.r = d
h.w = n
d.p = n
d.z = a
assert a.r.z.r.z.w.p.p.txt == "n"