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

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

n = A("n")
g = B("g")
f = B("f")
b = A("b")
a = B("a")

n.p = b
g.s = a
f.s = g
b.p = n
a.s = f
assert n.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.s: B = None
        self.txt = txt

n = A("n")
g = B("g")
f = B("f")
b = A("b")
a = B("a")

n.p = b
g.s = a
f.s = g
b.p = n
a.s = f
assert n.p.p.p.txt == "b"