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

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

l = A("l")
h = B("h")
d = B("d")
m = A("m")
g = B("g")
n = A("n")

l.z = m
h.x = g
h.r = n
d.x = h
d.r = n
m.z = l
g.x = d
g.r = n
n.z = l
assert g.x.r.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

l = A("l")
h = B("h")
d = B("d")
m = A("m")
g = B("g")
n = A("n")

l.z = m
h.x = g
h.r = n
d.x = h
d.r = n
m.z = l
g.x = d
g.r = n
n.z = l
assert g.x.r.z.z.z.z.txt == "m"