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

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

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

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

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

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

h.r = a
a.s = b
f.s = l
i.s = m
d.r = l
n.s = f
m.s = f
b.s = i
c.r = f
l.s = i
assert h.r.s.s.s.s.s.s.s.txt == "m"