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

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

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

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

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

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

j.w = a
j.y = m
m.p = a
a.w = f
a.y = h
f.w = j
f.y = m
h.p = j
assert m.p.y.p.w.w.txt == "f"