------
## 最终结论
```python
def min_add_to_make_valid(S: str) -> int:
    left_unmatched = 0  # Counter for unmatched '('
    right_needed = 0    # Counter for needed ')'
    
    for char in S:
        if char == '(':
            left_unmatched += 1
        elif char == ')':
            if left_unmatched > 0:
                left_unmatched -= 1
            else:
                right_needed += 1

    return left_unmatched + right_needed
```