def METHOD_NAME(self):
    assert self.layout.dag
    g = self.layout.layoutVertices
    layer_size = len(self)
    X = 0
    for i, j in zip(range(layer_size - 1), range(1, layer_size)):
        vi = self[i]
        vj = self[j]
        ni = [g[v].bar for v in self.neighbors(vi)]
        Xij = Xji = 0
        for nj in [g[v].bar for v in self.neighbors(vj)]:
            x = len([nx for nx in ni if nx > nj])
            Xij += x
            Xji += len(ni) - x
        if Xji < Xij:
            self[i] = vj
            self[j] = vi
            X += Xji
        else:
            X += Xij
    return X