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

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

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

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

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

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

a.z = m
k.p = a
k.r = g
m.p = a
m.r = g
g.p = a
g.r = m
assert g.r.r.r.txt == "m"