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

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

f = A("f")
b = B("b")
m = B("m")
d = A("d")
e = A("e")
a = A("a")

f.w = b
f.p = b
b.s = m
m.s = b
d.w = b
d.p = m
e.w = m
e.p = m
a.w = m
a.p = b
assert m.s.s.s.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.p: B = None
        self.txt = txt

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

f = A("f")
b = B("b")
m = B("m")
d = A("d")
e = A("e")
a = A("a")

f.w = b
f.p = b
b.s = m
m.s = b
d.w = b
d.p = m
e.w = m
e.p = m
a.w = m
a.p = b
assert m.s.s.s.s.s.s.txt == "m"