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

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

m = A("m")
f = B("f")
h = B("h")
d = A("d")
n = A("n")
l = B("l")
e = B("e")
b = B("b")

m.w = n
f.w = d
h.w = d
d.w = n
n.w = m
l.w = n
e.w = m
b.w = n
assert m.w.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.txt = txt

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

m = A("m")
f = B("f")
h = B("h")
d = A("d")
n = A("n")
l = B("l")
e = B("e")
b = B("b")

m.w = n
f.w = d
h.w = d
d.w = n
n.w = m
l.w = n
e.w = m
b.w = n
assert m.w.w.w.w.txt == "m"