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

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

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

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

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

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

m.w = a
m.p = i
i.p = m
i.r = k
a.w = m
a.p = i
k.p = a
k.r = i
assert i.r.p.p.r.txt == "k"