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

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

d = A("d")
g = B("g")
m = A("m")
f = B("f")

d.x = g
d.r = g
g.v = m
g.z = d
m.x = g
m.r = f
f.v = m
f.z = d
assert m.r.v.x.v.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.r: B = None
        self.txt = txt

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

d = A("d")
g = B("g")
m = A("m")
f = B("f")

d.x = g
d.r = g
g.v = m
g.z = d
m.x = g
m.r = f
f.v = m
f.z = d
assert m.r.v.x.v.txt == "m"