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

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

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

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

m = A("m")
b = B("b")
f = C("f")
n = D("n")
k = D("k")

m.r = k
m.z = f
b.x = m
f.o = m
n.x = k
n.p = f
k.x = n
k.p = f
assert m.z.o.z.txt == "
class A:
    def __init__(self, txt: str):
        self.r: D = None
        self.z: C = None
        self.txt = txt

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

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

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

m = A("m")
b = B("b")
f = C("f")
n = D("n")
k = D("k")

m.r = k
m.z = f
b.x = m
f.o = m
n.x = k
n.p = f
k.x = n
k.p = f
assert m.z.o.z.txt == "f"