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

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

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

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

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

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

m.r = e
b.p = e
h.r = a
e.p = b
g.r = a
f.r = e
a.p = b
assert b.p.p.p.p.p.p.p.txt == "e"