Long but easy solution :)
\n/**\n * @param {number[]} nums\n * @return {number}\n */\nconst semiOrderedPermutation = function (nums) {\n    const n = nums.length\n    if (nums[0] === 1 &
```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nconst semiOrderedPermutation = function (nums) {\n    const n = nums.length\n    if (nums[0] === 1 && nums[n - 1] === n) return 0\n    \n    let cn = 0\n    \n    let i = nums.indexOf(1)\n    while (nums[0] !== 1 && i >= 0) {\n        const curr = nums[i - 1]\n        \n        nums[i - 1] = nums[i]\n        nums[i] = curr\n        \n        i -= 1\n        cn += 1\n    }\n    \n    if (nums[0] === 1 && nums[n - 1] === n) return cn\n    i = nums.indexOf(n)\n    while (nums[n - 1] !== n && i < n) {\n        const curr = nums[i]\n        \n        nums[i] = nums[i + 1]\n        nums[i + 1] = curr\n        \n        i += 1\n        cn += 1\n    }\n    \n    return cn\n};\n```
['JavaScript']