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

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

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

j.x = f
j.r = m
m.z = j
m.o = g
f.z = j
f.o = m
g.z = j
g.o = m
assert g.o.o.o.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.r: B = None
        self.txt = txt

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

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

j.x = f
j.r = m
m.z = j
m.o = g
f.z = j
f.o = m
g.z = j
g.o = m
assert g.o.o.o.txt == "m"