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

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

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

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

e = A("e")
k = B("k")
b = C("b")
d = D("d")
c = B("c")
m = D("m")

e.w = m
k.p = m
k.r = m
k.w = e
b.p = c
d.w = m
c.p = d
c.r = d
c.w = e
m.w = d
assert b.p.w.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: D = None
        self.txt = txt

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

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

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

e = A("e")
k = B("k")
b = C("b")
d = D("d")
c = B("c")
m = D("m")

e.w = m
k.p = m
k.r = m
k.w = e
b.p = c
d.w = m
c.p = d
c.r = d
c.w = e
m.w = d
assert b.p.w.w.w.w.txt == "m"