def period(n, a):
    """Compute the period of a^kmod n)"""
    remainder_list = []
    k = 1
    while True:
        r = len(remainder_list)
        if r > 128:
            return None
        remainder = a ** k % n
        if remainder in remainder_list:
            return r
        else:
            remainder_list.append(remainder)
            k += 1