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

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

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

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

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

d.x = n
n.x = m
m.p = d
m.x = d
i.w = d
assert d.x.x.p.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

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

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

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

d.x = n
n.x = m
m.p = d
m.x = d
i.w = d
assert d.x.x.p.x.txt == "n"