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

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

m = A("m")
e = B("e")
b = A("b")
j = A("j")
a = B("a")
f = B("f")
k = B("k")
h = A("h")
d = B("d")
g = A("g")

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

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

m = A("m")
e = B("e")
b = A("b")
j = A("j")
a = B("a")
f = B("f")
k = B("k")
h = A("h")
d = B("d")
g = A("g")

m.o = a
m.r = j
e.p = m
b.o = d
b.r = h
j.o = f
j.r = g
a.p = m
f.p = h
k.p = m
h.o = a
h.r = b
d.p = m
g.o = k
g.r = m
assert k.p.r.o.p.o.p.o.p.txt == "m"