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

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

m = A("m")
k = B("k")
n = A("n")
d = B("d")

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

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

m = A("m")
k = B("k")
n = A("n")
d = B("d")

m.p = k
m.w = n
k.x = m
n.p = k
n.w = m
d.x = n
assert k.x.p.x.p.txt == "k"