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

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

m = A("m")
d = B("d")
n = A("n")
k = B("k")
c = B("c")
b = B("b")
i = B("i")

m.r = n
m.s = c
d.p = c
n.r = m
n.s = d
k.p = b
c.p = d
b.p = i
i.p = c
assert m.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.s: B = None
        self.txt = txt

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

m = A("m")
d = B("d")
n = A("n")
k = B("k")
c = B("c")
b = B("b")
i = B("i")

m.r = n
m.s = c
d.p = c
n.r = m
n.s = d
k.p = b
c.p = d
b.p = i
i.p = c
assert m.r.r.r.txt == "n"