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

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

i = A("i")
g = B("g")
n = A("n")

i.x = n
g.z = n
n.x = i
assert n.x.x.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.txt = txt

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

i = A("i")
g = B("g")
n = A("n")

i.x = n
g.z = n
n.x = i
assert n.x.x.x.txt == "i"