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

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

f = A("f")
l = B("l")
b = B("b")
a = B("a")
c = B("c")
d = A("d")
k = A("k")
m = B("m")

f.z = k
l.z = a
b.z = a
a.z = m
c.z = b
d.z = k
k.z = d
m.z = a
assert k.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

f = A("f")
l = B("l")
b = B("b")
a = B("a")
c = B("c")
d = A("d")
k = A("k")
m = B("m")

f.z = k
l.z = a
b.z = a
a.z = m
c.z = b
d.z = k
k.z = d
m.z = a
assert k.z.z.z.txt == "d"