```python
def first(arr, x, n):
    """
    Finds the first position of an element x in a sorted array arr.

    Args:
        arr: A sorted list of elements.
        x: The element to search for.
        n: The number of elements in the array.

    Returns:
        The index of the first occurrence of x, or -1 if x is not found.
    """
    low = 0
    high = n - 1
    first_occurrence = -1

    while low <= high:
        mid = low + (high - low) // 2

        if arr[mid] == x:
            first_occurrence = mid
            # Continue searching in the left half to find the first occurrence
            high = mid - 1
        elif arr[mid] < x:
            # The target element is in the right half
            low = mid + 1
        else:
            # The target element is in the left half
            high = mid - 1

    return first_occurrence
```