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

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

d = A("d")
m = B("m")
j = B("j")
k = A("k")
c = B("c")

d.x = k
d.z = k
m.r = d
j.r = k
k.x = d
k.z = d
c.r = d
assert k.z.x.x.z.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.z: A = None
        self.txt = txt

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

d = A("d")
m = B("m")
j = B("j")
k = A("k")
c = B("c")

d.x = k
d.z = k
m.r = d
j.r = k
k.x = d
k.z = d
c.r = d
assert k.z.x.x.z.x.txt == "d"