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

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

f = A("f")
a = B("a")
n = B("n")
g = A("g")
m = A("m")
d = A("d")
l = B("l")
e = B("e")

f.z = g
a.w = g
a.p = g
n.w = g
n.p = d
g.z = f
m.z = g
d.z = m
l.w = d
l.p = d
e.w = g
e.p = f
assert g.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

f = A("f")
a = B("a")
n = B("n")
g = A("g")
m = A("m")
d = A("d")
l = B("l")
e = B("e")

f.z = g
a.w = g
a.p = g
n.w = g
n.p = d
g.z = f
m.z = g
d.z = m
l.w = d
l.p = d
e.w = g
e.p = f
assert g.z.z.z.z.txt == "g"