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

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

d = A("d")
g = B("g")
k = B("k")
i = B("i")
h = A("h")
l = B("l")
e = A("e")

d.r = h
g.y = e
k.y = e
i.y = d
h.r = e
l.y = d
e.r = d
assert i.y.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.y: A = None
        self.txt = txt

d = A("d")
g = B("g")
k = B("k")
i = B("i")
h = A("h")
l = B("l")
e = A("e")

d.r = h
g.y = e
k.y = e
i.y = d
h.r = e
l.y = d
e.r = d
assert i.y.r.r.r.r.r.r.txt == "d"