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

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

m = A("m")
l = B("l")
a = A("a")
i = B("i")

m.r = a
m.z = a
l.r = a
l.p = m
a.r = m
a.z = m
i.r = m
i.p = m
assert i.r.z.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.z: A = None
        self.txt = txt

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

m = A("m")
l = B("l")
a = A("a")
i = B("i")

m.r = a
m.z = a
l.r = a
l.p = m
a.r = m
a.z = m
i.r = m
i.p = m
assert i.r.z.r.txt == "m"