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

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

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

d.p = g
g.o = m
a.p = m
k.p = m
n.p = m
e.p = g
m.o = g
assert n.p.o.o.o.o.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

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

d.p = g
g.o = m
a.p = m
k.p = m
n.p = m
e.p = g
m.o = g
assert n.p.o.o.o.o.txt == "m"