#include <iostream>
#include <string>
#include <deque>
#include <algorithm>

using namespace std;
int n,m;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    deque<int> dq;

    cin >> n >> m;
    
    for(int i=1; i<=n; i++) {
        dq.push_back(i);
    }
    int cnt=0;
    while(m--) {
        int t;
        cin >> t;
        int idx=find(dq.begin(),dq.end(),t)-dq.begin();
        while(dq.front()!=t) {
                if(idx<dq.size()-idx) {
                dq.push_back(dq.front());
                dq.pop_front();
            } else {
                dq.push_front(dq.back());
                dq.pop_back();
            }
            cnt++;
        }
        dq.pop_front();
    }
    cout << cnt;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: