def binary_search(a, x):
    mid = len(x) // 2

    while len(x) > 2:
        if x[mid] == a:
            return mid
        elif x[mid] < a:
            x = x[:mid]
        else:
            x = x[mid:]
        mid = len(x) // 2

    if x[0] == a or x[1] == a:
        return x.index(a)
    else:
        return -1

# Example usage:
a = 5
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = binary_search(a, x)
print(result)