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

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

m = A("m")
c = B("c")
a = B("a")
d = B("d")
e = A("e")
g = B("g")
j = B("j")
b = B("b")

m.r = d
c.w = b
a.w = b
d.w = a
e.r = c
g.w = b
j.w = d
b.w = a
assert c.w.w.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

m = A("m")
c = B("c")
a = B("a")
d = B("d")
e = A("e")
g = B("g")
j = B("j")
b = B("b")

m.r = d
c.w = b
a.w = b
d.w = a
e.r = c
g.w = b
j.w = d
b.w = a
assert c.w.w.w.w.w.txt == "b"