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

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

g = A("g")
d = B("d")
a = A("a")
k = B("k")

g.u = a
d.z = g
d.r = k
a.u = g
k.z = g
k.r = d
assert a.u.u.u.u.txt == "
class A:
    def __init__(self, txt: str):
        self.u: A = None
        self.txt = txt

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

g = A("g")
d = B("d")
a = A("a")
k = B("k")

g.u = a
d.z = g
d.r = k
a.u = g
k.z = g
k.r = d
assert a.u.u.u.u.txt == "a"