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

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

h = A("h")
j = B("j")
d = A("d")
i = A("i")
l = B("l")
k = A("k")
m = B("m")
n = A("n")

h.r = k
j.z = h
d.r = i
i.r = h
l.z = n
k.r = i
m.z = n
n.r = k
assert j.z.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.z: A = None
        self.txt = txt

h = A("h")
j = B("j")
d = A("d")
i = A("i")
l = B("l")
k = A("k")
m = B("m")
n = A("n")

h.r = k
j.z = h
d.r = i
i.r = h
l.z = n
k.r = i
m.z = n
n.r = k
assert j.z.r.r.r.txt == "h"