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

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

n = A("n")
j = B("j")
m = B("m")
b = A("b")
d = A("d")
g = A("g")

n.w = g
n.x = d
j.s = d
m.s = b
b.w = g
b.x = g
d.w = b
d.x = g
g.w = n
g.x = n
assert j.s.x.x.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.x: A = None
        self.txt = txt

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

n = A("n")
j = B("j")
m = B("m")
b = A("b")
d = A("d")
g = A("g")

n.w = g
n.x = d
j.s = d
m.s = b
b.w = g
b.x = g
d.w = b
d.x = g
g.w = n
g.x = n
assert j.s.x.x.w.w.txt == "n"