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

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

i = A("i")
d = B("d")
j = B("j")
g = B("g")
m = A("m")
h = B("h")

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

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

i = A("i")
d = B("d")
j = B("j")
g = B("g")
m = A("m")
h = B("h")

i.p = j
d.v = m
j.v = m
g.v = i
m.p = j
h.v = m
assert i.p.v.p.txt == "j"