def inverse_permutation(permutation, j):
    """ Inverse the permutation for given input j, that is, it finds i such that p[i] = j.

    >>> permutation = [1, 0, 3, 2]
    >>> inverse_permutation(permutation, 1)
    0
    >>> inverse_permutation(permutation, 0)
    1
    """
    for i, pi in enumerate(permutation):
        if pi == j:
            return i
    raise ValueError('inverse_permutation({}, {}) failed.'.format(
        permutation, j))
