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

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

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

g.x = m
g.p = d
m.r = a
l.x = m
l.p = d
i.x = m
i.p = d
n.x = d
n.p = m
f.x = d
f.p = m
d.r = l
j.x = d
j.p = m
a.x = m
a.p = d
assert g.x.r.p.r.x.r.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.p: B = None
        self.txt = txt

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

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

g.x = m
g.p = d
m.r = a
l.x = m
l.p = d
i.x = m
i.p = d
n.x = d
n.p = m
f.x = d
f.p = m
d.r = l
j.x = d
j.p = m
a.x = m
a.p = d
assert g.x.r.p.r.x.r.x.txt == "m"