#include <bits/stdc++.h>
#define maxn 500000
using namespace std;
int n,q,ans;
int p[maxn],pos[maxn];
bool inc(int i) {
    return i>=2 && i<=n && pos[i-1]>pos[i];
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n>>q;
    for(int i=1;i<=n;i++) {
        cin>>p[i];
        pos[p[i]]=i;
    }
    for(int i=2;i<=n;i++) ans+=inc(i);
    cout<<ans<<endl;
    while(q--) {
        int x,y;
        cin>>x>>y;
        set<int> s = {p[x],p[x]+1,p[y],p[y]+1};
        for(auto v:s) ans-=inc(v);
        swap(pos[p[x]],pos[p[y]]);
        swap(p[x],p[y]);
        for(auto v:s) ans+=inc(v);
        cout<<ans<<endl;
    }
    return 0;
}
