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

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

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

m.o = a
m.w = k
g.y = a
g.z = a
k.y = m
k.z = m
a.o = m
a.w = k
assert a.w.z.w.z.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.w: B = None
        self.txt = txt

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

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

m.o = a
m.w = k
g.y = a
g.z = a
k.y = m
k.z = m
a.o = m
a.w = k
assert a.w.z.w.z.txt == "m"