Answer
\n# Code\npython3 []\nclass Solution:\n    def semiOrderedPermutation(self, nums: List[int]) -> int:\n        n=len(nums)\n        mn, mx = nums.index(1) , nums
\n# Code\n```python3 []\nclass Solution:\n    def semiOrderedPermutation(self, nums: List[int]) -> int:\n        n=len(nums)\n        mn, mx = nums.index(1) , nums.index(n)     \n        return mn - mx + n - 1 - (mn > mx)\n```
['Python3']