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

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

a = A("a")
f = B("f")
m = B("m")
d = B("d")
i = A("i")

a.p = i
f.y = m
m.y = f
d.y = m
i.p = a
assert f.y.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

a = A("a")
f = B("f")
m = B("m")
d = B("d")
i = A("i")

a.p = i
f.y = m
m.y = f
d.y = m
i.p = a
assert f.y.y.y.y.txt == "f"