#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

/*struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];*/

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n+2){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}

int main(){
   // freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

/*struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];*/

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}

int main(){
   // freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],-1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],-1);
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 512

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x], 1 );
        trees[block[y]].add( arr[y], 1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

/*struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];*/

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n+2){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

/*LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}*/
LL queryUp(int x, int y){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < arr[x] ) res++;
        }
        return res;
    }else{
        for(int i = x+1, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < arr[x]  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,arr[x]-1 );
        }
        for(int i = max(1,bly*SQRTN); i<=y; i++){
            if( arr[i] < arr[x]  ) res++;
        }
    }
    return res;
}

LL queryDown(int x, int y){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] > arr[y] ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < arr[y]  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,arr[y]-1 );
        }
        for(int i = max(1,bly*SQRTN); i<y; i++){
            if( arr[i] < arr[y]  ) res++;
        }
    }
    return (y-x)-res;
}

int main(){
   // freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        res -= queryUp(x,y);
        res -= queryDown(x,y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        res += queryUp(x,y);
        res += queryDown(x,y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x], 1 );
        trees[block[y]].add( arr[y], 1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 512

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;

    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
    void init(vector<int> a) {
        init(a.size());
        for (size_t i = 0; i < a.size(); i++)
            add(i, a[i]);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1; i< min(n+1,(bl+1)*SQRTN); i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            cout << res << endl;
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x], 1 );
        trees[block[y]].add( arr[y], 1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 2100000
#define SQRTN 512

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;

    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
    void init(vector<int> a) {
        init(a.size());
        for (size_t i = 0; i < a.size(); i++)
            add(i, a[i]);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    LL res = 0;
    for(int i=0; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1; i< min(n+1,(bl+1)*SQRTN); i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
  //  freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            cout << res << endl;
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x], 1 );
        trees[block[y]].add( arr[y], 1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

template <typename T> inline void next_int(T &x) {
	x = 0; char c; bool neg = false;
	while (!isdigit(c = getchar())) if (c == '-') neg = true;
	do x = x*10 + c - 48; while (isdigit(c = getchar()));
	if (neg) x = -x;
}

template <typename T> inline void write_int(T x, char last = 0) {
	if (x < 0) putchar('-'), x = abs(x);
	char tmp[20]; int cnt = 0;
	while (x >= 10) tmp[cnt++] = x % 10 + 48, x /= 10;
	tmp[cnt] = x + 48;
	FORD(i, cnt, 0) putchar(tmp[i]);
	if (last) putchar(last);
}

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n+2){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

/*LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}*/
LL query(int x, int y, int val){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < val ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < val  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,val-1 );
        }
        for(int i = max(1,bly*SQRTN); i<=y; i++){
            if( arr[i] < val  ) res++;
        }
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    next_int(n), next_int(q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        LL tmp1 = query(x+1,y-1,arr[x]);
        LL tmp2 = query(x+1,y-1,arr[y]);
        res += (tmp2 - tmp1);
		if (x + 1 <= y - 1) res += ((LL)(y - x - 1) - tmp1), res -= ((LL)(y - x - 1) - tmp2);
        if( arr[x] > arr[y] )  res--;
        else res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        // << x <<  " " << y << " " << res << endl;
        write_int(res, '\n');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 2100000
#define SQRTN 512

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;

    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
    void init(vector<int> a) {
        init(a.size());
        for (size_t i = 0; i < a.size(); i++)
            add(i, a[i]);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1; i< min(n+1,(bl+1)*SQRTN); i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            cout << res << endl;
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x], 1 );
        trees[block[y]].add( arr[y], 1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

template <typename T> inline void next_int(T &x) {
	x = 0; char c; bool neg = false;
	while (!isdigit(c = getchar())) if (c == '-') neg = true;
	do x = x*10 + c - 48; while (isdigit(c = getchar()));
	if (neg) x = -x;
}

template <typename T> inline void write_int(T x, char last = 0) {
	if (x < 0) putchar('-'), x = abs(x);
	char tmp[20]; int cnt = 0;
	while (x >= 10) tmp[cnt++] = x % 10 + 48, x /= 10;
	tmp[cnt] = x + 48;
	FORD(i, cnt, 0) putchar(tmp[i]);
	if (last) putchar(last);
}

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    for (; idx <= n; idx += idx & -idx)
        bit[i][idx] += val;
}
int sum(int i , int idx){
    int res = 0;
	for (; idx; idx -= idx & -idx)
        res += bit[i][idx];
    return res;
}

/*LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}*/
LL query(int x, int y, int val){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < val ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < val  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,val-1 );
        }
        for(int i = max(1,bly*SQRTN); i<=y; i++){
            if( arr[i] < val  ) res++;
        }
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    next_int(n), next_int(q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        LL tmp1 = query(x+1,y-1,arr[x]);
        LL tmp2 = query(x+1,y-1,arr[y]);
        res += (tmp2 - tmp1);
		if (x + 1 <= y - 1) res += ((LL)(y - x - 1) - tmp1), res -= ((LL)(y - x - 1) - tmp2);
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        // << x <<  " " << y << " " << res << endl;
        res += (arr[x] > arr[y]), res -= (arr[x] < arr[y]);
        write_int(res, '\n');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

/*struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;
    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

FenwickTree trees[SQRTN];*/

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n+2){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

/*LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}*/
LL query(int x, int y, int val){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < val ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < val  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,val-1 );
        }
        for(int i = max(1,bly*SQRTN); i<=y; i++){
            if( arr[i] < val  ) res++;
        }
    }
    return res;
}

int main(){
   // freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        LL tmp1 = query(x+1,y-1,arr[x]);
        LL tmp2 = query(x+1,y-1,arr[y]);
        res += 2*tmp2 - 2*tmp1;
        if( arr[x] > arr[y] )  res--;
        else res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        // << x <<  " " << y << " " << res << endl;
        printf("%lld\n",res);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[MAXN / SQRT];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i + 1);
		gg.pb(i + 1);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), x + 1) - blocks[bl].begin());
			by += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), y + 1) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		if(a != b) swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[5000];
vi gg;
ll n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	swap(gg[l], gg[r]);
	upd(l / SQRT);
	upd(r / SQRT);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vll blocks[5000];
vll gg;
ll n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	swap(gg[l], gg[r]);
	upd(l / SQRT);
	upd(r / SQRT);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[MAXN / SQRT];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i + 1);
		gg.pb(i + 1);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		if(a != b) swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[5000];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[5000];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r + 1) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), x + 1) - blocks[bl].begin());
			by += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), y + 1) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[MAXN / SQRT];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i + 1);
		gg.pb(i + 1);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r + 1) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		if(a != b) swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[5000];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), x + 1) - blocks[bl].begin());
			by += sz(blocks[bl]) - (lower_bound(all(blocks[bl]), y + 1) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	upd(l / SQRT);
	upd(r / SQRT);
	swap(gg[l], gg[r]);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)(a).size()
#define all(c) (c).begin(), (c).end()
#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", 1.0*clock()/CLOCKS_PER_SEC)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair <int, int> pii;
typedef vector <vi> vvi;
typedef vector <pii> vpii;
typedef vector<string> vs;

const int INF = 100500;
const int MAXN = 2e5 + 9;
const int MOD = 1e9 + 7;
const int SQRT = 447;

vi blocks[5000];
vi gg;
int n, q, ans;

void build() {
	for(int i = 0; i < n; i++) {
		blocks[i / SQRT].pb(i);
		gg.pb(i);
	}
}

void upd(int num) {
	int cnt = sz(blocks[num]);
	blocks[num].clear();
	int st = num * SQRT;
	for(int i = st; i < st + cnt; i++) blocks[num].pb(gg[i]);
	sort(all(blocks[num]));
}

void swp(int l, int r) {
	int bx(0), by(0), lx(0), ly(0);
	int x = gg[l];
	int y = gg[r];
	for(int i = l + 1; i < r;) {
		int bl = i / SQRT;
		if(i % SQRT == 0 && i + SQRT <= r) {
			lx += lower_bound(all(blocks[bl]), x) - blocks[bl].begin();
			ly += lower_bound(all(blocks[bl]), y) - blocks[bl].begin();
			bx += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), x) - blocks[bl].begin());
			by += sz(blocks[bl]) - (upper_bound(all(blocks[bl]), y) - blocks[bl].begin());
			i += SQRT;
		}
		else {
			bx += gg[i] > x;
			by += gg[i] > y;
			lx += gg[i] < x;
			ly += gg[i] < y;
			i++;
		}
	}
	ans += (bx - lx + ly - by);
	if(gg[l] > gg[r]) ans--;
	else if(gg[l] < gg[r]) ans++;
	swap(gg[l], gg[r]);
	upd(l / SQRT);
	upd(r / SQRT);
}

void input() {
	cin >> n >> q;
	build();
	for(int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		if(a > b) swap(a, b);
		swp(a - 1, b - 1);
		cout << ans << endl;
	}
}

void solve() {

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("xxx.in", "r", stdin);
	freopen("xxx.out", "w", stdout);
#else
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
#endif
	input();
	solve();
	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 5;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t[maxn];
int n;
int lowbit(int x)
{
    return x & -x;
}
void Insert(int pos, int val)
{
    for(int i = pos; i <= n; i += lowbit(i))
        t[i].insert(val);
}
void Remove(int pos, int val)
{
    for(int i = pos; i <= n; i += lowbit(i))
        t[i].erase(val);
}
int QueryOrder(int pos, int val)
{
    int ret = 0;
    for(int i = pos; i > 0; i -= lowbit(i))
        ret += t[i].order_of_key(val);
    return ret;
}
int a[maxn];
int main()
{
    int q;
    scanf("%d %d", &n, &q);
    for(int i = 1; i <= n; i++)
    {
        Insert(i, i);
        a[i] = i;
    }
    LL ans = 0;
    while(q--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if(l == r)
        {
            printf("%lld\n", ans);
            continue;
        }
        if(l > r) swap(l, r);
        if(a[l] > a[r]) ans--;
        else ans++;
        if(l + 1 < r)
        {
            int sub = QueryOrder(r - 1, a[l]) - QueryOrder(l, a[l]);
            int add = QueryOrder(r - 1, a[r]) - QueryOrder(l, a[r]);
            ans += 2 * add - 2 * sub;
        }
        Remove(l, a[l]);
        Remove(r, a[r]);
        swap(a[l], a[r]);
        Insert(l, a[l]);
        Insert(r, a[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 8000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 50000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 42500000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if(l==r)
		{
			printf("%lld\n", ans);
			continue;
		}
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
const int maxn = 2e5 + 5;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t[maxn * 4];
void build(int id, int L, int R)
{
    for (int i = L; i <= R; i++) t[id].insert(i);
    if (L == R) return ;
    int mid = L + R >> 1;
    if (L <= mid) build(id << 1, L, mid);
    if (mid < R) build(id << 1 | 1, mid + 1, R);
}
int Rank(int id, int L, int R, int l, int r, int x)
{
    if (l <= L && R <= r) return t[id].order_of_key(x);
    else
    {
        int ret = 0;
        int mid = L + R >> 1;
        if (l <= mid) ret += Rank(id << 1, L, mid, l, r, x);
        if (mid < r) ret += Rank(id << 1 | 1, mid + 1, R, l, r, x);
        return ret;
    }
}
void Replace(int id, int L, int R, int pos, int x, int y)
{
    t[id].erase(x);
    t[id].insert(y);
    if (L == R) return ;
    int mid = L + R >> 1;
    if (pos <= mid) Replace(id << 1, L, mid, pos, x, y);
    else Replace(id << 1 | 1, mid + 1, R, pos, x, y);
}
int v[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    build(1, 1, n);
    long long ans = 0;
    for (int i = 1; i <= n; i++) v[i] = i;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int len = r - l - 1;
            int r1 = Rank(1, 1, n, l + 1, r - 1, v[l]);
            int r2 = Rank(1, 1, n, l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Replace(1, 1, n, l, v[l], v[r]);
        Replace(1, 1, n, r, v[r], v[l]);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 43000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int L[MAXN], R[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d %d", &L[i], &R[i]);
        maxn = max(maxn, R[i]);
    }
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
    {
        T[i] = Insert(T[i - 1], i, 1);
        v[i] = i;
    }
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    for (int i = 1; i <= m; i++)
    {
        int l = L[i], r = R[i];
        if (l == r)
        {
            printf("%lld\n", ans);
            continue;
        }
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 42000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 43000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int L[MAXN], R[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d %d", &L[i], &R[i]);
        maxn = max(maxn, R[i]);
        maxn = max(maxn,L[i]);
    }
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
    {
        T[i] = Insert(T[i - 1], i, 1);
        v[i] = i;
    }
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    for (int i = 1; i <= m; i++)
    {
        int l = L[i], r = R[i];
        if (l == r)
        {
            printf("%lld\n", ans);
            continue;
        }
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 32000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 45000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    maxn = n;
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
	{
		T[i] = Insert(T[i - 1], i, 1);
		v[i] = i;
	}
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
const int maxn = 2e5 + 5;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t[maxn * 4];
void build(int id, int L, int R)
{
    for(int i = L; i <= R; i++) t[id].insert(i);
    if(L == R) return ;
    int mid = L + R >> 1;
    build(id << 1, L, mid);
    build(id << 1 | 1, mid + 1, R);
}
int Rank(int id, int L, int R, int l, int r, int x)
{
    if(l <= L && R <= r)
    {
        int ret = t[id].order_of_key(x);
        return ret;
    }
    else
    {
        int ret = 0;
        int mid = L + R >> 1;
        if(l <= mid) ret += Rank(id << 1, L, mid, l, r, x);
        if(mid < r) ret += Rank(id << 1 | 1, mid + 1, R, l, r, x);
        return ret;
    }
}
void Insert(int id, int L, int R, int pos, int y)
{
    t[id].insert(y);
    if(L == R) return ;
    int mid = L + R >> 1;
    if(pos <= mid) Insert(id << 1, L, mid, pos, y);
    else Insert(id << 1 | 1, mid + 1, R, pos, y);
}
void Remove(int id, int L, int R, int pos, int y)
{
    t[id].erase(y);
    if(L == R) return ;
    int mid = L + R >> 1;
    if(pos <= mid) Remove(id << 1, L, mid, pos, y);
    else Remove(id << 1 | 1, mid + 1, R, pos, y);
}
int v[maxn];
struct FastIO
{
    static const int S = 1310720;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if(pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if(pos == len) return -1;
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while(c <= 32) c = xchar();
        for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline void wchar(int x)
    {
        if(wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(long long x)
    {
        if(x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while(x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while(n--) wchar(s[n]);
    }
    inline void wstring(const char *s)
    {
        while(*s) wchar(*s++);
    }
    ~FastIO()
    {
        if(wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
int main()
{
    int n = io.xuint(), m = io.xuint();
    build(1, 1, n);
    long long ans = 0;
    for(int i = 1; i <= n; i++) v[i] = i;
    while(m--)
    {
        int l = io.xuint(), r = io.xuint();
        if(l > r) swap(l, r);
        if(v[l] > v[r]) ans--;
        else if(v[l] < v[r]) ans++;
        if(l + 1 < r)
        {
            int r1 = Rank(1, 1, n, l + 1, r - 1, v[l]);
            int r2 = Rank(1, 1, n, l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Remove(1, 1, n, l, v[l]);
        Remove(1, 1, n, r, v[r]);
        Insert(1, 1, n, l, v[r]);
        Insert(1, 1, n, r, v[l]);
        swap(v[l], v[r]);
        io.wint(ans);
        io.wchar('\n');
    }
    return 0;
}


#include<bits/stdc++.h>
#define M 44000000
#define MAXN 200005
using namespace std;
int cnt[M], T[MAXN], s[MAXN], lson[M], rson[M];
int a[MAXN], t[MAXN], use[MAXN];
int tot, n, maxn;
int build(int l, int r)
{
    int root = tot++;
    cnt[root] = 0;
    lson[root] = root;
    rson[root] = root;
    return root;
}
int Insert(int root, int pos, int val)
{
    int newroot = tot++;
    int temp = newroot;
    cnt[newroot] = cnt[root] + val;
    int l = 1, r = maxn;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            lson[newroot] = lson[root], rson[newroot] = tot++;
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        cnt[newroot] = cnt[root] + val;
    }
    return temp;
}
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += cnt[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
void Modify(int x, int pos, int val)
{
    while (x <= n)
    {
        s[x] = Insert(s[x], pos, val);
        x += lowbit(x);
    }
}
int querank(int left, int right, int x)
{
    int left_root = T[left - 1], right_root = T[right];
    int l = 1, r = maxn;
    int i;
    int ret = 0;
    for (i = right; i; i -= lowbit(i)) use[i] = s[i];
    for (i = left - 1; i; i -= lowbit(i)) use[i] = s[i];
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (x <= mid)
        {
            r = mid;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = lson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            ret += sum(right) - sum(left - 1) + cnt[lson[right_root]] - cnt[lson[left_root]];
            l = mid + 1;
            for (i = left - 1; i; i -= lowbit(i)) use[i] = rson[use[i]];
            for (i = right; i; i -= lowbit(i)) use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return ret;
}
int v[MAXN];
int L[MAXN], R[MAXN];
int main()
{
    int m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d %d", &L[i], &R[i]);
        maxn = max(maxn, R[i]);
    }
    T[0] = build(1, maxn);
    for (int i = 1; i <= n; i++)
    {
        T[i] = Insert(T[i - 1], i, 1);
        v[i] = i;
    }
    for (int i = 1; i <= n; i++) s[i] = T[0];
    long long ans = 0;
    for (int i = 1; i <= m; i++)
    {
        int l = L[i], r = R[i];
        if (l == r)
        {
            printf("%lld\n", ans);
            continue;
        }
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = querank(l + 1, r - 1, v[l]);
            int r2 = querank(l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Modify(l, v[l], -1);
        Modify(l, v[r], 1);
        Modify(r, v[r], -1);
        Modify(r, v[l], 1);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
const int maxn = 2e5 + 5;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t[maxn * 4];
void build(int id, int L, int R)
{
    for (int i = L; i <= R; i++) t[id].insert(i);
    if (L == R) return ;
    int mid = L + R >> 1;
    build(id << 1, L, mid);
    build(id << 1 | 1, mid + 1, R);
}
int Rank(int id, int L, int R, int l, int r, int x)
{
    if (l <= L && R <= r)
    {
        int ret = t[id].order_of_key(x);
        return ret;
    }
    else
    {
        int ret = 0;
        int mid = L + R >> 1;
        if (l <= mid) ret += Rank(id << 1, L, mid, l, r, x);
        if (mid < r) ret += Rank(id << 1 | 1, mid + 1, R, l, r, x);
        return ret;
    }
}
void Insert(int id, int L, int R, int pos, int y)
{
    t[id].insert(y);
    if (L == R) return ;
    int mid = L + R >> 1;
    if (pos <= mid) Insert(id << 1, L, mid, pos, y);
    else Insert(id << 1 | 1, mid + 1, R, pos, y);
}
void Remove(int id, int L, int R, int pos, int y)
{
    t[id].erase(y);
    if (L == R) return ;
    int mid = L + R >> 1;
    if (pos <= mid) Remove(id << 1, L, mid, pos, y);
    else Remove(id << 1 | 1, mid + 1, R, pos, y);
}
int v[maxn];
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    build(1, 1, n);
    long long ans = 0;
    for (int i = 1; i <= n; i++) v[i] = i;
    while (m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        if (l > r) swap(l, r);
        if (v[l] > v[r]) ans--;
        else if (v[l] < v[r]) ans++;
        if (l + 1 < r)
        {
            int r1 = Rank(1, 1, n, l + 1, r - 1, v[l]);
            int r2 = Rank(1, 1, n, l + 1, r - 1, v[r]);
            ans += 2 * (-r1 + r2);
        }
        Remove(1, 1, n, l, v[l]);
        Remove(1, 1, n, r, v[r]);
        Insert(1, 1, n, l, v[r]);
        Insert(1, 1, n, r, v[l]);
        swap(v[l], v[r]);
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<int,int> pii;

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> my_super_set;

#define DIM 200007

#define MAXNUMBER 1000000003

int a[DIM];
//my_super_set T[4*DIM];
int n;

const int INF = 1000000009;

//��������� "�������" ������
struct node{
    node *left, *right; //��������� �� ����
    int key, cnt; //���� �� �������, ������� �������� � �������
    long long prior;

    node(int newkey) { //����������� ����������� ���� ���������
        key = newkey; //�������� ��������� ����
        cnt = 1; //������� �������� � �������
        prior = (long long)rand()*(long long)rand(); //������� ������� ��������
        left = right = NULL; //��� � ����� ������
    }
};

typedef node* pnode; //��������� ��� ����� pnode - �������� �� node, ��� �� ������ ����� node*

//������� ������� ������ � ������� � ������� root
int get_cnt(pnode root) {
    if(root == NULL) return 0;
    else return root->cnt;
}

//������� �������� � ������ ���� ��������� ����
void update(pnode root) {
    if(root != NULL) root->cnt = 1 + get_cnt(root->left) + get_cnt(root->right);
}

//split ������ ����� ������ ��� ����� �������� � ���� �� ����� ��������. ������� ���� � ���� ��������� �����.
pair<pnode, pnode> split(pnode root, int key){
    if(root == NULL) return {NULL,NULL}; //���� ������  �����,  �� ��������� ��������� ������

    if(root->key < key) { //���� ���� ����� ����� ����� ������ - �� �������� ������� ����
        pair<pnode, pnode> sp = split(root->right, key); //�������� ����� ����������� �� �������� �����
        root->right = sp.first; //������ ��� ����� ������� ������� ������ ������
        update(root); //���������  �������� ���� ����������� ����
        return {root, sp.second}; //��������� ��������� ���������
    } else { //����������� ������� ��� ��� �������
        pair<pnode, pnode> sp = split(root->left, key);
        root->left = sp.second;
        update(root); //���������  �������� ���� ����������� ����
        return {sp.first,  root}; //��� ��� �� ������, �� ������� ���������� ��� �����������
    }
}

//����� ��� ���������� ������ L,R ����� �� �Ѳ ����� � L �� ����� �� ����� � R
pnode merge(pnode L, pnode R) {
    if(L == NULL) return R; //���� ��� ������ ����� - �� ��������� ������  ������� �������
    if(R == NULL) return L; //���������
    //����� ������� ���� ������ ������ �� ����. ������� �� ����� �� ������� � ��� ������ �������
    if(L->prior > R->prior) { //���� ������� ������ � ����� ������,
        L->right = merge(L->right, R); //�� ������� ������� ���� L � ������ �������
        update(L); //���������  �������� ���� ����������� ����
        return L; //��������� ����� ������� ������
    } else { //����������� ������
        R->left = merge(L, R->left);
        update(R); //���������  �������� ���� ����������� ����
        return R;
    }
}

void insert(pnode &root, pnode &t) {
    if(root == NULL) {
        root = t;
        update(root);
        return;
    }
    if(root->prior > t->prior) {
        if(t->key < root->key)
            insert(root->left, t);
        else
            insert(root->right, t);
        update(root);
        return;
    }
    pair<pnode, pnode> temp = split(root, t->key);
    t->left = temp.first;
    t->right = temp.second;
    root = t;
    update(root);
}

void addnode(pnode &root, int key) {
    pnode t = new node(key);
    insert(root, t);
}

/*
//���������� ���� ������� � ������ (����� ���� �������� �� ������ ���� ��������������)
void insert(pnode &root,  int  key) { // &root - �� ����� ��������
    pair<pnode, pnode> sp = split(root, key); //�������� ������ �� ����� ���� ���� ��������
    //����� sp.first �� ����� �� ����� �� key , � ����� ������ � sp.second
    //������� sp.first, ������ � ���� ���� �������, � sp.second
    root = merge(merge(sp.first, new node(key)), sp.second);
}
*/

//����� ��� �������� � ������� root, ��������� ������� ���'��� (���� �� �����)
void dispose(pnode root) {
    if(root == NULL) return;
    dispose(root->left); //������� ����
    dispose(root->right);
    delete(root); //���������� ���'��� ��� ������. (������������  root = new node()
}

void erase(pnode &root, int k) {
    if(root == NULL) return;
    else if(k < root->key) erase(root->left, k);
    else if(k > root->key) erase(root->right, k);
    else root = merge(root->left, root->right);
    update(root);
}

/*

//��������� �������� � ������ �
void erase(pnode &root, int k) {
    pair<pnode, pnode> sp1, sp2;
    sp1 = split(root, k); //������� ������ �� ����� �
    sp2 = split(sp1.second, k+1); //�������� ����� ������ �� ����� �+1

    dispose(sp2.first);//��������� ���'��� � ������ � ������� �

    root = merge(sp1.first, sp2.second); //����� ������ �� �� ������ ������ �

}
*/

int find_kth(pnode root, int k) {
    if(get_cnt(root) < k) return -INF;

    int inright = get_cnt(root->right);

    if(inright + 1 == k) return root->key;

    if(inright >= k) return find_kth(root->right, k);

    return find_kth(root->left, k - (inright + 1));

}

int order_of_key(pnode root, int k) {
    if(root == NULL) return 0;
    if(root->key == k) return get_cnt(root->left);
    else if(k < root->key) return order_of_key(root->left, k);
    else return get_cnt(root->left) + 1 + order_of_key(root->right, k);
}
///////////////////////////////////////////////////////

pnode T[4*DIM];

void buildtree(int v, int tl, int tr) {

    for(int i = tl ; i <= tr; i++) addnode(T[v], a[i]);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr) / 2;
    buildtree(2*v+1, tl, tm);
    buildtree(2*v+2, tm+1, tr);
}

void addpoint(int v, int tl, int tr, int pos, int val, int type) {
    if(pos < tl || tr < pos) return;

    if(type == 1) addnode(T[v], val);
    else erase(T[v], val);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr)/2;
    if(pos <= tm) addpoint(2*v+1, tl, tm, pos, val, type);
    else addpoint(2*v+2, tm+1, tr, pos, val, type);
}

int query(int v, int tl, int tr, int L, int R, int x, int type) {
    if(R < tl || tr < L) return 0;

    if(L <= tl && tr <= R) {
        if(type == 1)return order_of_key(T[v],x);
        else return (tr-tl+1) - order_of_key(T[v], x+1);
    }

    int tm = (tl+tr) / 2;
    return query(2*v+1, tl, tm, L, R, x, type) + query(2*v+2, tm+1, tr, L, R, x, type);
}

int changepoint(int pos, int newval) {
    int c = 0;
    c -= query(0, 1, n, 1, pos-1, a[pos], 2);
    c -= query(0, 1, n, pos+1, n, a[pos], 1);
    addpoint(0, 1, n, pos, a[pos], 2);
    a[pos] = newval;
    addpoint(0, 1, n, pos, newval, 1);
    c += query(0, 1, n, 1, pos-1, a[pos], 2);
    c += query(0, 1, n, pos+1, n, a[pos], 1);
    return c;
}

int makeswap(int L, int R) {
    if(L == R) return 0;
    int t1,t2,t3,t4,add;
    t1 = query(0, 1, n, L+1, R-1, a[L], 1);
    t2 = R-L-1 - t1;

    t4 = query(0, 1, n, L+1, R-1, a[R], 1);
    t3 = R-L-1 - t4;
    add = -t1 + t2 + - t3 + t4;
    if(a[L] < a[R]) add++;
    else add--;

    int oldl = a[L], oldr = a[R];

    addpoint(0, 1, n, L, a[L], 2);
    addpoint(0, 1, n, R, a[R], 2);
    addpoint(0, 1, n, L, a[R], 1);
    addpoint(0, 1, n, R, a[L], 1);

    swap(a[L], a[R]);

    return add;
}

int main() {
  //freopen("input.txt", "r", stdin);

  srand(clock());

  int xx, yy, m, oldxx, oldyy;
  long long res;

  scanf("%d %d", &n, &m);
  //n = 200000; m = 50000;
  for(int i = 1; i <= n; i++) a[i] = i;
  buildtree(0, 1, n);
//return 0;
  res = 0;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &xx, &yy);
/*
    xx = rand() % 200000 + 1;
    yy = rand() % 200000 + 1;
*/
    if(xx > yy) swap(xx,yy);
    res += makeswap(xx,yy);
    printf("%lld\n", res);
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIM 200007
#define MAXNUMBER 200003

#define NUMBLOCK 507

#define BSIZE 2000

using namespace std;

int a[DIM];
int n;
int F[NUMBLOCK][MAXNUMBER+7];
int bs[NUMBLOCK];

int sumprefix(int bnum, int y) {
    y++;
    int res = 0;
    for(int i = y; i > 0; i -= i & -i)
        res += F[bnum][i];
    return res;
}

void add(int bnum, int y, int delta) {
    y++;
    for(int i = y; i <= MAXNUMBER; i += i & -i)
        F[bnum][i] += delta;
}

void changevalue(int pos, int newval) {
    add(pos/BSIZE, a[pos], -1);
    add(pos/BSIZE, newval, 1);
    a[pos] = newval;
}

int query(int x1, int x2, int yy) {

    if(x1 > x2) return 0;

    int b1,b2,res = 0;
    b1 = x1 / BSIZE;
    b2 = x2 / BSIZE;

    if(b1 == b2) {
        for(int j = x1; j <= x2; j++)
            if(a[j] < yy) res++;
    } else {
        for(int j = x1; j < (b1+1)*BSIZE; j++)
            if(a[j] < yy) res++;

        for(int j = b1+1; j <= b2-1; j++)
            res += sumprefix(j, yy-1);

        for(int j = (b2*BSIZE); j <= x2; j++)
            if(a[j] < yy) res++;

    }
    return res;
}

int makeswap(int L, int R) {
    if(L == R) return 0;
    int t1,t2,t3,t4,add;
    t1 = query(L+1, R-1, a[L]);
    t2 = R-L-1 - t1;

    t4 = query(L+1, R-1, a[R]);
    t3 = R-L-1 - t4;
    add = -t1 + t2 + - t3 + t4;
    if(a[L] < a[R]) add++;
    else add--;

    int oldl = a[L], oldr = a[R];

    changevalue(L, oldr);
    changevalue(R, oldl);
    //swap(a[L], a[R]);

    return add;
}

int main() {
  //freopen("input.txt", "r", stdin);

  int xx, yy, m, oldxx, oldyy;
  long long res;

  scanf("%d %d", &n, &m);
  //n = 200000; m = 50000;
  for(int i = 1; i <= n; i++) a[i] = i;
  for(int i = 1; i <= n; i++) {
    add(i / BSIZE, a[i], 1);
    bs[i/BSIZE]++;
  }

  res = 0;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &xx, &yy);
/*
    xx = rand() % 200000 + 1;
    yy = rand() % 200000 + 1;
*/
    if(xx > yy) swap(xx,yy);
    res += makeswap(xx,yy);
    printf("%lld\n", res);
  }

  return 0;
}