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

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

a = A("a")
l = B("l")
k = A("k")
m = B("m")
b = B("b")

a.y = k
l.s = m
l.r = a
k.y = a
m.s = b
m.r = k
b.s = m
b.r = k
assert b.r.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

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

a = A("a")
l = B("l")
k = A("k")
m = B("m")
b = B("b")

a.y = k
l.s = m
l.r = a
k.y = a
m.s = b
m.r = k
b.s = m
b.r = k
assert b.r.y.y.y.txt == "a"