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

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

i = A("i")
g = B("g")
m = B("m")
h = A("h")
b = B("b")

i.o = m
g.x = h
g.r = b
m.x = i
m.r = b
h.o = g
b.x = h
b.r = m
assert g.r.r.x.o.r.txt == "
class A:
    def __init__(self, txt: str):
        self.o: B = None
        self.txt = txt

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

i = A("i")
g = B("g")
m = B("m")
h = A("h")
b = B("b")

i.o = m
g.x = h
g.r = b
m.x = i
m.r = b
h.o = g
b.x = h
b.r = m
assert g.r.r.x.o.r.txt == "b"