------
## 最终结论
```python
def next_greater_element(n):
    def integer_to_array(n):
        return [int(digit) for digit in str(n)]

    def find_first_ascending_pair(arr):
        i = len(arr) - 2
        while i >= 0 and arr[i] >= arr[i + 1]:
            i -= 1
        return i

    def find_min_greater(arr, i):
        min_index = i + 1
        for j in range(i + 1, len(arr)):
            if arr[j] > arr[i] and (arr[j] < arr[min_index] or arr[min_index] == arr[i]):
                min_index = j
        return min_index

    def reverse_after_index(arr, i):
        arr[i + 1:] = arr[i + 1:][::-1]
        return arr

    def check_32bit_integer(arr):
        new_number = int(''.join(map(str, arr)))
        if 0 < new_number <= 2**31 - 1:
            return new_number
        else:
            return -1

    arr = integer_to_array(n)
    i = find_first_ascending_pair(arr)

    if i >= 0:
        min_index = find_min_greater(arr, i)
        arr[i], arr[min_index] = arr[min_index], arr[i]
        result_arr = reverse_after_index(arr, i)
        result = check_32bit_integer(result_arr)
    else:
        result = -1

    return result

# Example usage
print(next_greater_element(12)) # Output: 21
print(next_greater_element(21)) # Output: -1
```