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

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

g = A("g")
f = B("f")
n = B("n")
e = A("e")
i = B("i")
m = A("m")

g.p = m
f.s = g
f.z = i
n.s = m
n.z = i
e.p = g
i.s = m
i.z = f
m.p = e
assert i.z.s.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

g = A("g")
f = B("f")
n = B("n")
e = A("e")
i = B("i")
m = A("m")

g.p = m
f.s = g
f.z = i
n.s = m
n.z = i
e.p = g
i.s = m
i.z = f
m.p = e
assert i.z.s.p.p.p.txt == "g"