1599_I. Desert

You are given an undirected graph of N nodes and M edges, E_1, E_2, ... E_M.

A connected graph is a cactus if each of it's edges belogs to at most one simple cycle. A graph is a desert if each of it's connected components is a cactus. 

Find the number of pairs (L, R), (1 ≤ L ≤ R ≤ M) such that, if we delete all the edges except for E_L, E_{L+1}, ... E_R, the graph is a desert. 

Input

The first line contains two integers N and M (2 ≤ N ≤ 2.5 × 10^5, 1 ≤ M ≤ 5 × 10^5). Each of the next M lines contains two integers. The i-th line describes the i-th edge. It contains integers U_i and V_i, the nodes connected by the i-th edge (E_i=(U_i, V_i)). It is guaranteed that 1 ≤ U_i, V_i ≤ N and U_i ≠ V_i. 

Output

The output contains one integer number – the answer.

Examples

Input


5 6
1 2
2 3
3 4
4 5
5 1
2 4


Output


20

Input


2 3
1 2
1 2
1 2


Output


5

Note

In the second example: Graphs for pairs (1, 1), (2, 2) and (3, 3) are deserts because they don't have any cycles. Graphs for pairs (1, 2) and (2, 3) have one cycle of length 2 so they are deserts.

Example input:
2 3
1 2
1 2
1 2

5 6
1 2
2 3
3 4
4 5
5 1
2 4


Example output:
5

20


Tags: data structures
graphs
Difficulty rating: 2700

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 250050;
const int M = 750050;
const int inf = 1e9 + 7;
int u[M], v[M];
int fa[M], go[M][2], cnt[M], sum[M], val[M], lzy[M], tag[M], type[M], mn[M];
void upd(int x, int v) {
  if (!x) return;
  sum[x] += cnt[x] * v;
  if (type[x] == 1) val[x] += v;
  lzy[x] += v;
}
void rev(int x) {
  swap(go[x][0], go[x][1]);
  tag[x] ^= 1;
}
void push(int x) {
  if (lzy[x]) {
    upd(go[x][0], lzy[x]);
    upd(go[x][1], lzy[x]);
    lzy[x] = 0;
  }
  if (tag[x]) {
    rev(go[x][0]);
    rev(go[x][1]);
    tag[x] = 0;
  }
}
void pull(int x) {
  if (!x) return;
  cnt[x] = cnt[go[x][0]] + type[x] + cnt[go[x][1]];
  sum[x] = sum[go[x][0]] + val[x] + sum[go[x][1]];
  mn[x] = min(min(mn[go[x][0]], x), mn[go[x][1]]);
}
int pd(int x) { return go[fa[x]][0] == x ? 0 : go[fa[x]][1] == x ? 1 : -1; }
void rot(int x) {
  int y = fa[x], z = fa[y], p = pd(x), q = pd(y), w = go[x][p ^ 1];
  if (~q) go[z][q] = x;
  go[x][p ^ 1] = y;
  go[y][p] = w;
  if (w) fa[w] = y;
  fa[y] = x;
  fa[x] = z;
  pull(y);
  pull(x);
}
void cl(int x) {
  if (~pd(x)) cl(fa[x]);
  push(x);
}
void splay(int x, int y = 0) {
  for (cl(x); ~pd(x) && fa[x] != y; rot(x))
    if (~pd(fa[x]) && fa[fa[x]] != y) rot(pd(x) == pd(fa[x]) ? fa[x] : x);
}
void fix(int x, int c) {
  splay(x);
  go[x][1] = c;
  pull(x);
}
void access(int x) {
  for (fix(x, 0); fa[x]; rot(x)) fix(fa[x], x);
}
void mkrt(int x) {
  access(x);
  rev(x);
}
void path(int x, int y) {
  mkrt(x);
  access(y);
  splay(x, y);
}
bool link(int x, int y, int i) {
  path(x, y);
  if (!fa[x]) {
    fa[x] = i;
    fa[i] = y;
    return true;
  }
  if (sum[y] > 0) return false;
  int mne = mn[y];
  splay(mne);
  fa[go[mne][0]] = 0;
  go[mne][0] = 0;
  fa[go[mne][1]] = 0;
  go[mne][1] = 0;
  path(x, y);
  fa[x] = i;
  fa[i] = y;
  path(u[mne], v[mne]);
  upd(v[mne], 1);
  return true;
}
void cut(int x, int y, int i) {
  path(x, y);
  if (sum[y] > 0) {
    upd(y, -1);
  } else {
    fa[go[x][1]] = 0;
    go[x][1] = 0;
    pull(x);
    fa[x] = 0;
    go[y][0] = 0;
    pull(y);
  }
}
int main() {
  mn[0] = inf;
  int n, m;
  scanf("%i %i", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%i %i", &u[i], &v[i]);
    u[i] += m;
    v[i] += m;
    type[i] = 1;
  }
  int ptr = 1;
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    while (ptr <= m && link(u[ptr], v[ptr], ptr)) {
      ptr++;
    }
    ans += ptr - i;
    cut(u[i], v[i], ptr);
  }
  printf("%lld\n", ans);
  return 0;
}