Write a function that takes a positive integer and returns the next smaller positive integer containing the same digits.
For example:
```python
next_smaller(21) == 12
next_smaller(531) == 513
next_smaller(2071) == 2017
```
Return -1 (for `Haskell`: return `Nothing`, for `Rust`: return `None`), when there is no smaller number that contains the same digits. Also return -1 when the next smaller number with the same digits would require the leading digit to be zero.
```python 
next_smaller(9) == -1
next_smaller(135) == -1
next_smaller(1027) == -1  # 0721 is out since we don't write numbers with leading zeros
```
```ruby 
next_smaller(9) == -1
next_smaller(135) == -1
next_smaller(1027) == -1  # 0721 is out since we don't write numbers with leading zeros
```
 * some tests will include very large numbers.
 * test data only employs positive integers.
*The function you write for this challenge is the inverse of this kata: "[Next bigger number with the same digits](http://www.codewars.com/kata/next-bigger-number-with-the-same-digits)."*
def next_smaller(n):
    nums = list(map(int, list(str(n))))
    seen = []
    for i in reversed(range(1, len(nums))):
        if nums[i - 1] > nums[i]:
            seen.append(nums[i])
            seen.append(nums[i-1])
            seen = sorted(seen, reverse=True)
            for j in range(len(seen)):
                if seen[j] < nums[i-1]:
                    winner = seen.pop(j)
                    break
            if winner == 0 and i == 1:
                return -1

            nums = nums[:i-1]
            nums.append(winner)
            nums.extend(seen)
            return int(''.join(map(str, nums)))
        else:
            seen.append(nums[i])

    return -1
def next_smaller(n):
    nums = list(map(int, list(str(n))))
    seen = []
    for i in reversed(range(1, len(nums))):
        if nums[i - 1] > nums[i]:
            seen.append(nums[i])
            seen.append(nums[i-1])
            seen = sorted(seen, reverse=True)
            for j in range(len(seen)):
                if seen[j] < nums[i-1]:
                    winner = seen.pop(j)
                    break
            if winner == 0 and i == 1:
                return -1

            nums = nums[:i-1]
            nums.append(winner)
            nums.extend(seen)
            return int(''.join(map(str, nums)))
        else:
            seen.append(nums[i])

    return -1