import sympy


def order_from_anf(anf):
    """
    Counts the minimum order of a feedback shift register.

    This is accomplished by stringifying the symbols in the expression
    and finding the greatest index.

    Parameters
    ----------
    anf : algebraic normal form
        A SymPy expression describing the algebraic normal form of a
        feedback shift register.  Must be using integer symbols named
        `x_k`, where `k=0, 1, ...`.

    Returns
    -------
    n : integer
        The minimum order of `anf`.

    Examples
    --------
    >>> x0, x1, x2 = sympy.symbols('x_0 x_1 x_2', integer=True)
    >>> order_from_anf(x0 + x1)
    2
    >>> order_from_anf(x0 + x2)
    3
    """
    n = 0
    for arg in anf.args:
        if isinstance(arg, sympy.Symbol):
            m = int(str(arg).split('_')[-1]) + 1
            n = m if m > n else n
        else:
            m = order_from_anf(arg)
            n = m if m > n else n
    return n