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

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

j = A("j")
f = B("f")
m = B("m")
d = A("d")
k = B("k")
n = A("n")

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

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

j = A("j")
f = B("f")
m = B("m")
d = A("d")
k = B("k")
n = A("n")

j.u = n
j.w = f
f.p = k
m.p = k
d.u = n
d.w = f
k.p = m
n.u = j
n.w = k
assert m.p.p.p.p.p.p.txt == "m"