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

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

m = A("m")
f = B("f")
k = B("k")
b = B("b")
g = A("g")
d = A("d")

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

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

m = A("m")
f = B("f")
k = B("k")
b = B("b")
g = A("g")
d = A("d")

m.p = f
f.w = d
k.w = m
b.w = d
g.p = f
d.p = k
assert b.w.p.w.txt == "m"