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

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

a = A("a")
m = B("m")
g = B("g")
k = A("k")

a.p = k
m.y = k
g.y = a
k.p = a
assert a.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.y: A = None
        self.txt = txt

a = A("a")
m = B("m")
g = B("g")
k = A("k")

a.p = k
m.y = k
g.y = a
k.p = a
assert a.p.p.p.txt == "k"