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

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

l = A("l")
a = B("a")
g = B("g")
j = A("j")
m = B("m")

l.s = j
a.o = g
a.z = m
g.o = a
g.z = a
j.s = l
m.o = g
m.z = g
assert m.o.o.z.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.txt = txt

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

l = A("l")
a = B("a")
g = B("g")
j = A("j")
m = B("m")

l.s = j
a.o = g
a.z = m
g.o = a
g.z = a
j.s = l
m.o = g
m.z = g
assert m.o.o.z.txt == "m"