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

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

m = A("m")
h = B("h")
n = B("n")
i = B("i")
f = A("f")
b = A("b")
j = A("j")
g = A("g")

m.p = f
m.y = n
h.p = f
h.w = g
n.p = f
n.w = g
i.p = m
i.w = j
f.p = g
f.y = n
b.p = j
b.y = n
j.p = g
j.y = n
g.p = b
g.y = h
assert n.w.y.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.y: B = None
        self.txt = txt

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

m = A("m")
h = B("h")
n = B("n")
i = B("i")
f = A("f")
b = A("b")
j = A("j")
g = A("g")

m.p = f
m.y = n
h.p = f
h.w = g
n.p = f
n.w = g
i.p = m
i.w = j
f.p = g
f.y = n
b.p = j
b.y = n
j.p = g
j.y = n
g.p = b
g.y = h
assert n.w.y.p.p.txt == "g"