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

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

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

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

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

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

f.p = m
f.z = j
m.y = b
g.p = b
g.z = j
j.p = b
j.z = d
d.p = m
d.z = f
b.y = m
assert f.z.z.p.y.txt == "b"