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

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

e = A("e")
m = B("m")
g = A("g")
j = A("j")

e.y = m
m.x = g
g.y = m
j.y = m
assert m.x.y.x.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

e = A("e")
m = B("m")
g = A("g")
j = A("j")

e.y = m
m.x = g
g.y = m
j.y = m
assert m.x.y.x.txt == "g"