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

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

f = A("f")
d = B("d")
m = B("m")
k = A("k")
j = B("j")
a = B("a")

f.z = k
d.z = f
d.r = j
m.z = k
m.r = d
k.z = f
j.z = k
j.r = m
a.z = k
a.r = d
assert m.r.r.z.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: A = None
        self.r: B = None
        self.txt = txt

f = A("f")
d = B("d")
m = B("m")
k = A("k")
j = B("j")
a = B("a")

f.z = k
d.z = f
d.r = j
m.z = k
m.r = d
k.z = f
j.z = k
j.r = m
a.z = k
a.r = d
assert m.r.r.z.z.z.z.txt == "f"