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

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

h = A("h")
l = B("l")
m = A("m")
c = B("c")
f = A("f")
d = A("d")
b = A("b")
a = B("a")

h.r = m
l.w = h
l.z = b
m.r = h
c.w = f
c.z = f
f.r = b
d.r = m
b.r = f
a.w = d
a.z = d
assert m.r.r.r.r.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

h = A("h")
l = B("l")
m = A("m")
c = B("c")
f = A("f")
d = A("d")
b = A("b")
a = B("a")

h.r = m
l.w = h
l.z = b
m.r = h
c.w = f
c.z = f
f.r = b
d.r = m
b.r = f
a.w = d
a.z = d
assert m.r.r.r.r.r.r.r.r.txt == "m"