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

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

m = A("m")
d = B("d")
g = B("g")
f = A("f")

m.z = d
m.u = g
d.p = f
g.p = f
f.z = g
f.u = d
assert d.p.u.p.txt == "
class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.u: B = None
        self.txt = txt

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

m = A("m")
d = B("d")
g = B("g")
f = A("f")

m.z = d
m.u = g
d.p = f
g.p = f
f.z = g
f.u = d
assert d.p.u.p.txt == "f"