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

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

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

f.r = m
i.u = c
i.z = f
a.u = g
a.z = m
c.u = a
c.z = f
j.u = c
j.z = m
g.u = j
g.z = m
h.r = m
m.r = f
assert a.z.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.u: B = None
        self.z: A = None
        self.txt = txt

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

f.r = m
i.u = c
i.z = f
a.u = g
a.z = m
c.u = a
c.z = f
j.u = c
j.z = m
g.u = j
g.z = m
h.r = m
m.r = f
assert a.z.r.r.r.r.txt == "m"