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

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

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

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

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

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

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