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.y: B = None
        self.txt = txt

g = A("g")
j = B("j")
k = B("k")
i = A("i")
d = B("d")

g.r = i
g.x = i
j.y = k
k.y = d
i.r = g
i.x = g
d.y = j
assert d.y.y.y.y.y.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.y: B = None
        self.txt = txt

g = A("g")
j = B("j")
k = B("k")
i = A("i")
d = B("d")

g.r = i
g.x = i
j.y = k
k.y = d
i.r = g
i.x = g
d.y = j
assert d.y.y.y.y.y.txt == "k"