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

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

m = A("m")
n = B("n")
a = A("a")
b = B("b")
j = B("j")
i = B("i")

m.z = a
n.y = j
n.p = m
a.z = m
b.y = n
b.p = a
j.y = b
j.p = a
i.y = j
i.p = m
assert m.z.z.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.y: B = None
        self.p: A = None
        self.txt = txt

m = A("m")
n = B("n")
a = A("a")
b = B("b")
j = B("j")
i = B("i")

m.z = a
n.y = j
n.p = m
a.z = m
b.y = n
b.p = a
j.y = b
j.p = a
i.y = j
i.p = m
assert m.z.z.z.z.z.z.txt == "m"