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

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

a = A("a")
k = B("k")
j = A("j")
c = A("c")
l = A("l")
g = B("g")
h = A("h")
e = A("e")
d = B("d")
f = B("f")

a.x = k
a.p = l
k.p = f
j.x = k
j.p = l
c.x = d
c.p = a
l.x = g
l.p = c
g.p = k
h.x = g
h.p = l
e.x = d
e.p = h
d.p = f
f.p = d
assert g.p.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.p: A = None
        self.txt = txt

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

a = A("a")
k = B("k")
j = A("j")
c = A("c")
l = A("l")
g = B("g")
h = A("h")
e = A("e")
d = B("d")
f = B("f")

a.x = k
a.p = l
k.p = f
j.x = k
j.p = l
c.x = d
c.p = a
l.x = g
l.p = c
g.p = k
h.x = g
h.p = l
e.x = d
e.p = h
d.p = f
f.p = d
assert g.p.p.p.p.txt == "f"