class A:
    def __init__(self, txt: str):
        self.y: D = None
        self.o: C = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.v: B = None
        self.r: B = None
        self.txt = txt

class D:
    def __init__(self, txt: str):
        self.v: C = None
        self.y: C = None
        self.o: A = None
        self.txt = txt

d = A("d")
i = B("i")
h = C("h")
m = D("m")

d.y = m
d.o = h
i.v = d
h.v = i
h.r = i
m.v = h
m.y = h
m.o = d
assert m.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.y: D = None
        self.o: C = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.v: B = None
        self.r: B = None
        self.txt = txt

class D:
    def __init__(self, txt: str):
        self.v: C = None
        self.y: C = None
        self.o: A = None
        self.txt = txt

d = A("d")
i = B("i")
h = C("h")
m = D("m")

d.y = m
d.o = h
i.v = d
h.v = i
h.r = i
m.v = h
m.y = h
m.o = d
assert m.v.v.v.txt == "d"