```
class Solution(object):
def nextGreaterElement(self, n):
"""
:type n: int
:rtype: int
"""
def swap(s, i, j):
temp = s[i]
s[i] = s[j]
s[j] = temp
return s
s = list(str(n))
for i in range(len(s) - 1, -1, -1):
for j in range(len(s) - 1, i, -1):
if s[j] > s[i]:
swap(s, i, j)
right = s[i+1:]
right.sort()
ans = s[:i+1] + right
if int("".join(ans)) > 2**31-1:
return -1
return int(''.join(ans))
return -1
```