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

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

g = A("g")
d = B("d")
f = B("f")
k = A("k")
n = A("n")
m = B("m")
l = B("l")
h = B("h")
c = B("c")
e = B("e")

g.y = k
d.r = m
f.r = m
k.y = g
n.y = k
m.r = f
l.r = m
h.r = f
c.r = f
e.r = d
assert n.y.y.y.y.y.y.y.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

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

g = A("g")
d = B("d")
f = B("f")
k = A("k")
n = A("n")
m = B("m")
l = B("l")
h = B("h")
c = B("c")
e = B("e")

g.y = k
d.r = m
f.r = m
k.y = g
n.y = k
m.r = f
l.r = m
h.r = f
c.r = f
e.r = d
assert n.y.y.y.y.y.y.y.y.y.y.txt == "g"