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

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

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

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

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

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

k.p = n
n.q = m
m.p = n
assert m.p.q.p.txt == "n"