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

d = A("d")
j = B("j")
f = A("f")
e = B("e")
m = A("m")
c = A("c")
a = B("a")
g = A("g")
h = B("h")

d.y = j
j.r = h
f.y = a
e.r = h
m.y = a
c.y = e
a.r = e
g.y = h
h.r = a
assert d.y.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

d = A("d")
j = B("j")
f = A("f")
e = B("e")
m = A("m")
c = A("c")
a = B("a")
g = A("g")
h = B("h")

d.y = j
j.r = h
f.y = a
e.r = h
m.y = a
c.y = e
a.r = e
g.y = h
h.r = a
assert d.y.r.r.r.r.txt == "h"