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

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

h = A("h")
b = B("b")
k = A("k")
a = A("a")
d = B("d")

h.r = a
b.w = a
k.r = h
a.r = h
d.w = k
assert b.w.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

h = A("h")
b = B("b")
k = A("k")
a = A("a")
d = B("d")

h.r = a
b.w = a
k.r = h
a.r = h
d.w = k
assert b.w.r.r.r.r.txt == "a"