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

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

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

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

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

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

d.p = l
d.x = k
i.r = a
a.r = k
l.p = m
l.x = k
k.r = a
m.p = d
m.x = a
assert a.r.r.r.r.r.txt == "k"