[C++] Simple Solution
Code
# Code
```cpp []
class Solution {
public:
    int semiOrderedPermutation(vector<int>& A) {
        int ans = 0;
        int idx = 0;
        for(int i=0; i<A.size(); i++) {
            if(A[i] == 1) {
                idx = i;
                break;
            }
            ans++;
        }

        while(A[0] != 1) {
            int tmp = A[idx];
            A[idx] = A[idx-1];
            A[idx-1] = tmp;
            idx--;
        }

        idx = 0;
        for(int i=0; i<A.size(); i++) {
            if(A[i] == A.size()) {
                idx = i;
                break;
            }
        }

        while(A[A.size()-1] != A.size()) {
            int tmp = A[idx];
            A[idx] = A[idx+1];
            A[idx+1] = tmp;
            idx++;
            ans++;
        }

        return ans;        
    }
};
```
['C++']