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

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

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

h = A("h")
n = B("n")
m = C("m")
c = A("c")

h.v = c
n.p = m
n.s = c
m.x = c
m.r = n
m.p = n
c.v = h
assert h.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.v: A = None
        self.txt = txt

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

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

h = A("h")
n = B("n")
m = C("m")
c = A("c")

h.v = c
n.p = m
n.s = c
m.x = c
m.r = n
m.p = n
c.v = h
assert h.v.v.v.txt == "c"