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

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

d = A("d")
f = B("f")
m = B("m")
k = B("k")
j = B("j")
a = B("a")
l = A("l")

d.o = k
d.r = j
f.r = l
m.r = d
k.r = d
j.r = l
a.r = d
l.o = f
l.r = k
assert l.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.o: B = None
        self.r: B = None
        self.txt = txt

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

d = A("d")
f = B("f")
m = B("m")
k = B("k")
j = B("j")
a = B("a")
l = A("l")

d.o = k
d.r = j
f.r = l
m.r = d
k.r = d
j.r = l
a.r = d
l.o = f
l.r = k
assert l.r.r.r.txt == "j"