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

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

g = A("g")
f = B("f")
h = B("h")
m = B("m")
b = A("b")

g.w = b
f.w = h
f.y = m
h.w = m
h.y = m
m.w = f
m.y = h
b.w = g
assert b.w.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: B = None
        self.y: B = None
        self.txt = txt

g = A("g")
f = B("f")
h = B("h")
m = B("m")
b = A("b")

g.w = b
f.w = h
f.y = m
h.w = m
h.y = m
m.w = f
m.y = h
b.w = g
assert b.w.w.w.w.w.txt == "g"