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

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

f = A("f")
c = B("c")
d = A("d")
l = B("l")
j = B("j")
m = A("m")

f.p = l
f.r = d
c.z = f
d.p = j
d.r = f
l.z = f
j.z = d
m.p = c
m.r = d
assert f.p.z.r.r.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.r: A = None
        self.txt = txt

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

f = A("f")
c = B("c")
d = A("d")
l = B("l")
j = B("j")
m = A("m")

f.p = l
f.r = d
c.z = f
d.p = j
d.r = f
l.z = f
j.z = d
m.p = c
m.r = d
assert f.p.z.r.r.p.z.txt == "f"