Python3 Program
Code


# Code
```python3 []
class Solution:
    def semiOrderedPermutation(self, nums: List[int]) -> int:
        count = 0
        for i in range(len(nums)):
            if nums[i] == 1:
                while i > 0:
                    nums[i], nums[i - 1] = nums[i - 1], nums[i]
                    count += 1
                    i -= 1
                break
        for i in range(len(nums)):
            if nums[i] == len(nums):
                while i < len(nums) - 1:
                    nums[i], nums[i + 1] = nums[i + 1], nums[i]
                    count += 1
                    i += 1
                break
        return count
```
['Python3']