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

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

d = A("d")
e = B("e")
m = B("m")
k = B("k")
a = B("a")
g = A("g")

d.r = k
d.v = m
e.v = g
m.v = g
k.v = g
a.v = d
g.r = m
g.v = m
assert e.v.v.v.r.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.v: B = None
        self.txt = txt

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

d = A("d")
e = B("e")
m = B("m")
k = B("k")
a = B("a")
g = A("g")

d.r = k
d.v = m
e.v = g
m.v = g
k.v = g
a.v = d
g.r = m
g.v = m
assert e.v.v.v.r.v.v.txt == "m"