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

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

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

d.p = h
b.x = d
b.s = k
m.p = a
k.x = m
k.s = e
g.x = m
g.s = b
a.p = h
e.x = h
e.s = b
h.p = d
f.x = a
f.s = b
assert e.s.s.s.s.x.p.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.x: A = None
        self.s: B = None
        self.txt = txt

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

d.p = h
b.x = d
b.s = k
m.p = a
k.x = m
k.s = e
g.x = m
g.s = b
a.p = h
e.x = h
e.s = b
h.p = d
f.x = a
f.s = b
assert e.s.s.s.s.x.p.p.p.p.txt == "d"