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

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

n = A("n")
b = B("b")
f = A("f")
m = A("m")
d = A("d")
a = B("a")

n.x = f
b.r = m
f.x = m
m.x = f
d.x = n
a.r = m
assert d.x.x.x.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.txt = txt

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

n = A("n")
b = B("b")
f = A("f")
m = A("m")
d = A("d")
a = B("a")

n.x = f
b.r = m
f.x = m
m.x = f
d.x = n
a.r = m
assert d.x.x.x.x.txt == "f"