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

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

i = A("i")
l = B("l")
g = B("g")
m = A("m")
h = B("h")

i.r = g
i.p = m
l.v = i
g.v = m
m.r = l
m.p = i
h.v = m
assert g.v.r.v.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.p: A = None
        self.txt = txt

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

i = A("i")
l = B("l")
g = B("g")
m = A("m")
h = B("h")

i.r = g
i.p = m
l.v = i
g.v = m
m.r = l
m.p = i
h.v = m
assert g.v.r.v.r.txt == "g"