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

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

m = A("m")
g = B("g")
a = B("a")
e = B("e")

m.x = a
m.p = e
g.z = m
a.z = m
e.z = m
assert m.p.z.x.z.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.p: B = None
        self.txt = txt

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

m = A("m")
g = B("g")
a = B("a")
e = B("e")

m.x = a
m.p = e
g.z = m
a.z = m
e.z = m
assert m.p.z.x.z.txt == "m"