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

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

l = A("l")
k = B("k")
i = A("i")
b = A("b")
j = B("j")
f = B("f")

l.r = i
k.p = j
k.r = f
i.r = l
b.r = l
j.p = f
j.r = f
f.p = k
f.r = k
assert j.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.p: B = None
        self.r: B = None
        self.txt = txt

l = A("l")
k = B("k")
i = A("i")
b = A("b")
j = B("j")
f = B("f")

l.r = i
k.p = j
k.r = f
i.r = l
b.r = l
j.p = f
j.r = f
f.p = k
f.r = k
assert j.r.r.r.txt == "f"