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

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

n = A("n")
a = B("a")
i = B("i")
f = A("f")
k = A("k")
j = A("j")

n.r = j
n.x = k
a.q = n
i.q = k
f.r = j
f.x = n
k.r = j
k.x = n
j.r = f
j.x = n
assert n.r.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.x: A = None
        self.txt = txt

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

n = A("n")
a = B("a")
i = B("i")
f = A("f")
k = A("k")
j = A("j")

n.r = j
n.x = k
a.q = n
i.q = k
f.r = j
f.x = n
k.r = j
k.x = n
j.r = f
j.x = n
assert n.r.r.r.r.r.txt == "j"