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

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

a = A("a")
h = B("h")
f = B("f")
k = B("k")
m = A("m")

a.x = f
h.u = m
h.o = a
f.u = a
f.o = a
k.u = m
k.o = m
m.x = f
assert f.o.x.o.x.u.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

a = A("a")
h = B("h")
f = B("f")
k = B("k")
m = A("m")

a.x = f
h.u = m
h.o = a
f.u = a
f.o = a
k.u = m
k.o = m
m.x = f
assert f.o.x.o.x.u.txt == "a"