def inverse(phi, e):
    """
    Use the Bezout law to calculate the inverse of e to the modulus of phi.

    :param phi:
    :param e:
    :return: an integer d st. d*e = 1 (mod phi)
    """
    s, t, sn, tn, r = 1, 0, 0, 1, 1
    while r != 0:
        q = phi // e
        r = phi - q * e
        st, tt = sn * (-q) + s, tn * (-q) + t
        s, t = sn, tn
        sn, tn = st, tt
        phi = e
        e = r
    return t