def fq_invert(P, a):
    """Ivnert int value using extended euclidian algorithm for inversion"""
    p = P
    x0, x1, y0, y1 = 1, 0, 0, 1
    while p != 0:
        q, a, p = a // p, p, a % p
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return x0 % P
