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

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

a = A("a")
i = B("i")
b = A("b")
g = B("g")

a.u = b
i.z = g
i.p = a
b.u = a
g.z = i
g.p = b
assert a.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: B = None
        self.p: A = None
        self.txt = txt

a = A("a")
i = B("i")
b = A("b")
g = B("g")

a.u = b
i.z = g
i.p = a
b.u = a
g.z = i
g.p = b
assert a.u.u.u.txt == "b"