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

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

c = A("c")
b = B("b")
f = B("f")
j = A("j")
g = A("g")
h = A("h")

c.x = h
b.z = h
b.v = f
f.z = g
f.v = b
j.x = g
g.x = c
h.x = c
assert b.v.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.txt = txt

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

c = A("c")
b = B("b")
f = B("f")
j = A("j")
g = A("g")
h = A("h")

c.x = h
b.z = h
b.v = f
f.z = g
f.v = b
j.x = g
g.x = c
h.x = c
assert b.v.v.v.v.txt == "b"