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

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

h = A("h")
a = B("a")
m = B("m")

h.p = m
a.y = m
a.s = h
m.y = a
m.s = h
assert m.s.p.s.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

h = A("h")
a = B("a")
m = B("m")

h.p = m
a.y = m
a.s = h
m.y = a
m.s = h
assert m.s.p.s.txt == "h"