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

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

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

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

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

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

b.y = i
a.r = n
m.y = i
f.y = a
n.r = a
d.y = a
i.r = n
assert a.r.r.r.r.r.txt == "n"