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

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

g = A("g")
k = B("k")
i = B("i")
c = A("c")
b = A("b")
d = A("d")

g.w = d
k.u = i
k.w = c
i.u = k
i.w = g
c.w = g
b.w = c
d.w = b
assert g.w.w.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.txt = txt

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

g = A("g")
k = B("k")
i = B("i")
c = A("c")
b = A("b")
d = A("d")

g.w = d
k.u = i
k.w = c
i.u = k
i.w = g
c.w = g
b.w = c
d.w = b
assert g.w.w.w.w.w.txt == "d"