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

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

n = A("n")
m = B("m")
g = B("g")
f = A("f")

n.p = m
n.t = f
m.w = g
g.w = m
f.p = m
f.t = n
assert f.p.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.t: A = None
        self.txt = txt

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

n = A("n")
m = B("m")
g = B("g")
f = A("f")

n.p = m
n.t = f
m.w = g
g.w = m
f.p = m
f.t = n
assert f.p.w.w.txt == "m"