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

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

h = A("h")
a = B("a")
m = B("m")
d = B("d")
i = A("i")

h.o = i
h.w = m
a.s = m
m.s = d
d.s = m
i.o = h
i.w = a
assert h.w.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.w: B = None
        self.txt = txt

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

h = A("h")
a = B("a")
m = B("m")
d = B("d")
i = A("i")

h.o = i
h.w = m
a.s = m
m.s = d
d.s = m
i.o = h
i.w = a
assert h.w.s.s.s.txt == "d"