Python3 easy solution
Since we can change neighboring elements, the result will be the summation of the indices(idx_fst_nm + n - 1 - idx_lst_nm)  of the desired elements to their ref
Since we can change neighboring elements, the result will be the summation of the indices(**idx_fst_nm + n - 1 - idx_lst_nm**)  of the desired elements to their reference points. \nif idx_fst_nm > idx_lst_nm then we must subtract 1, since in the end they will meet and 1 less action will be needed\n\n\n**Complexity\nTime complexity: O(n)\nSpace complexity: O(1)**\n\n\n```\ndef semiOrderedPermutation(self, nums: List[int]) -> int:\n\n\tn = len(nums)\n\tidx_fst_nm, idx_lst_nm = nums.index(1), nums.index(n)\n\n\tans = idx_fst_nm + n - 1 - idx_lst_nm\n\treturn ans - 1 if idx_fst_nm > idx_lst_nm else ans\n```\n\n
['Array', 'Math', 'Python3']